如何组合两个datagridview?

时间:2013-10-23 02:31:21

标签: vb.net datagridview

我是VB.net的新手。我想组合两个datagridviews,我从CSV文件中获取这两个数据。例如,我有Datagridview1:

  Dim sr As System.IO.StreamReader = Nothing
    Dim dt As New DataTable

    Try
        readFileName = "LNU010F" & "_" & dateParam.ToString("MM-dd-yyyy") & ".csv"
        sr = New System.IO.StreamReader("C:\Documents and Settings\ucpb\Desktop\Sample CSV data\" + readFileName)
        Dim newline() As String = sr.ReadLine.Split("|"c)
        dt.Columns.AddRange({New DataColumn(newline(11)), New DataColumn(newline(48))})

        While (Not sr.EndOfStream)
            newline = sr.ReadLine.Split("|"c)
            Dim newrow As DataRow = dt.NewRow
            newrow.ItemArray = {newline(11), newline(48)}                
            dt.Rows.Add(newrow)
        End While

        DataGridViewCSV1.DataSource = dt

这是我从第一个代码得到的。 http://i.stack.imgur.com/QaJZ1.png

我有Datagridview2:

 Dim delimeter As String = txtDelimiter.Text
    Dim fileReader As System.IO.StreamReader = Nothing
    Dim dt As New DataTable

    Try
        fileName = txtName.Text & "_" & dateParam.ToString("MM-dd-yyyy")
        readFileName = txtName.Text & "_" & dateParam.ToString("MM-dd-yyyy") & ".csv"
        fileReader = New System.IO.StreamReader("C:\Documents and Settings\ucpb\Desktop\Sample CSV data\" + readFileName)

        Dim readColumn As String = fileReader.ReadLine
        Dim splitColumn As String() = readColumn.Split(delimeter)

        For Each arr As String In splitColumn
            dt.Columns.Add(arr)
        Next

        Dim readRow As String = fileReader.ReadLine
        Dim splitRow As String()

        While readRow <> ""
            splitRow = readRow.Split(delimeter)

            dt.Rows.Add(splitRow)
            readRow = fileReader.ReadLine()
        End While

        DataGridViewCSV2.DataSource = dt

这是我从第二个代码得到的。 http://i.stack.imgur.com/r0SmT.png

我想要的是在Datagridview2的末尾添加 UID (Datagridview1)。 Acctno 是关键,Datagridview2应该具有与第一个datagridview中相同的UID,因为数据未在datagridview2中正确排列。任何答案将不胜感激。谢谢。

0 个答案:

没有答案