如何将数据加载到正确的数据网格视图列中 [英] How do I load data into correct data grid view columns

查看:69
本文介绍了如何将数据加载到正确的数据网格视图列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,



我从Treeview(即用户点击节点)和TextBox(即用户使用按钮点击复制文本)加载数据并自动生成数字列和日期和时间列数据(参见图像)。该数据被传递给Listview,Listview可以由用户更改。然后,用户将Listview中的数据保存到文本文件中。然后,数据从文本文件加载并显示在DataGridView中,以便用户可以操作数据(即过滤等)。但是,正如您从图像中看到的那样,DGV中的前8行正确加载,而不会在Snippet列中插入文本片段。但是,当我使用Snippet加载数据时,日期和时间数据将添加到添加的行下方的新/单独行中。我一直在尝试在谷歌搜索和调试等许多小时没有成功?我在这里包含(我认为)相关的代码片段,并希望有人可以帮我解决这个问题?我很感激任何帮助! :-)(对不起,我想上传一张图片来显示问题,但我想这不可能吗?)。 DGV列按以下顺序排列:Number Column = 0(#);映射列= 1;片段列= 2;日期和时间栏= 3.



我尝试过:



Hi Folks,

I am loading data from a Treeview (i.e. user clicks node) and a TextBox (i.e. user copies text using button click) and auto-generating number column and Date and Time column data (See Image). This data is passed to a Listview which can be altered by the user. Then, the user saves the data from Listview to a text file. Then, the data is loaded from the text file and displayed in a DataGridView so the user can manipulate the data (i.e. Filtering, etc.). However, as you can see from the image, the first 8 rows in the DGV load correctly without inserting a text Snippet in the Snippet column. But, when I load the data with a Snippet, the Date and Time data is added to a new/separate row underneath the added row. I have been trying for many hours searching on Google and debugging, etc., with no success? I include (what I think are) relevant code snippets here and hope someone can help me to resolve this problem? I am grateful for any help! :-) (Sorry, I wanted to upload an image to show the problem, but I guess that's not possible here?). The DGV columns are in the following order: Number Column = 0 (#); Mapping Column = 1; Snippet Column = 2; Date and Time column = 3.

What I have tried:

'DGV SETUP:

'Create an unbound DataGridView by declaring a column count.
        DataGridView7.ColumnCount = 4
        DataGridView7.ColumnHeadersVisible = True

        ' Set the column header style.
        Dim columnHeaderStyle As New DataGridViewCellStyle()

        columnHeaderStyle.BackColor = Color.Beige
        columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
        DataGridView7.ColumnHeadersDefaultCellStyle = columnHeaderStyle

'Creates columns and sets column header names.
            DataGridView7.Columns(0).Name = "#"
            DataGridView7.Columns(1).Name = "Mapping Items"
            DataGridView7.Columns(2).Name = "Snippet"
            DataGridView7.Columns(3).Name = "Date and Time"

 With DataGridView7
                ' .AutoResizeColumnHeadersHeight
                .Columns("#").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
                .Columns("Mapping Items").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("Snippet").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("Date and Time").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            End With

'SAVE LISTVIEW TO A SPECIFIC FILE:

' Creates a <CSV> File
        Using csv As New System.IO.StreamWriter("C:\Users\CurrentUser\Documents\MappingData.txt", True) 'What does True do here???
            For Each Item As ListViewItem In ListView1.Items
                csv.WriteLine(String.Format("{0};{1};{2};{3}",
                                            Item.SubItems(0).Text,
                                            Item.SubItems(1).Text,
                                            Item.SubItems(2).Text,
                                            Item.SubItems(3).Text))
            Next
        End Using

'FORM LOAD EVENT: LOAD MAPPING DGV FROM TEXT FILE:

    Private Sub LOADDGV()

        Using stream As System.IO.FileStream = System.IO.File.OpenRead("C:\Users\CurrentUser\Documents\MappingData.txt")
            Using reader As New System.IO.StreamReader(stream)

                Dim line As String = reader.ReadLine()

                While (line IsNot Nothing)

                    Dim columns = line.Split(";") 'Text separator which splits to display in DGV columns (i.e. ;:," ", etc.)
                    line = reader.ReadLine()

                    Dim index = DataGridView7.Rows.Add(line)

                    DataGridView7.Rows(index).SetValues(columns)

                End While

            End Using
        End Using

    End Sub

推荐答案

VB.NET中的CSV到DataGrid - Stack Overflow [ ^ ]



Chekout这个答案。
CSV to DataGrid in VB.NET - Stack Overflow[^]

Chekout this answer.


这篇关于如何将数据加载到正确的数据网格视图列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆