如何合并列/单元格datagridview vb.net

时间:2016-03-10 15:42:56

标签: vb.net datagridview

在我按照我想要的方式添加一些值之后,我一直在尝试合并datagridview中的列,但有时它可以工作,有时它不会。如果你添加2个datagridviews,我可以尝试这个,一个有6列,另一个有1列。

我正在使用额外的单元格来“存储”一些值,然后删除它们。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      For i = 0 To DataGridView1.RowCount - 1
        For j = 0 To DataGridView1.RowCount - 1
            If i > j Then
                'If DataGridView1(0, i).Value = DataGridView1(0, j).Value Then
                If DataGridView1(0, i).Value = DataGridView1(0, j).Value Then
                    DataGridView1(3, i).Value = DataGridView1(1, i).Value + DataGridView1(1, j).Value
                End If
            End If
            If i < j Then
                'If DataGridView1(0, i).Value = DataGridView1(0, j).Value Then
                If DataGridView1(0, i).Value = DataGridView1(0, j).Value Then
                    DataGridView1(4, i).Value = DataGridView1(1, i).Value + DataGridView1(1, j).Value
                End If
            End If
            If i = j Then
                DataGridView1(5, i).Value = DataGridView1(1, i).Value
            End If
        Next
    Next
    Call lezgo()
End Sub
Private Sub lezgo()
    Dim i As Integer
    For i = 0 To DataGridView1.ColumnCount - 1
    Next
    If i > 0 Then
        DataGridView2.Rows.Add(i)
    End If
    For i = 0 To DataGridView1.ColumnCount - 1
        DataGridView2(0, i).Value = DataGridView1(3, i).Value + DataGridView1(4, i).Value + DataGridView1(5, i).Value
         Next

    DataGridView1.Columns.Remove(Column4)
    DataGridView1.Columns.Remove(Column5)
    DataGridView1.Columns.Remove(Column6)
End Sub

我知道我正在做的事情真的很糟糕,但它有点奏效。但现在我想优化这个,我无法弄清楚这一点..

当我无法执行此操作时,它会显示错误“System.InvalidCastException”。 我用Google搜索了一下,我看到没有类似的项目...... Tks!

编辑:

1| A| one
2| B| two
3| A| three
2| C| four
1| D| five
4| B| six

我想 - &gt;

1| A| one      |AD  |   |  |               |AD       this is dgv2
2| B| two      |BC  |CE |  |               |BCE
3| A| three    |    |   |A |               |A
2| C| four     |BC  |CE |  |               |BCE
1| D| five     |AD  |   |  |               |AD
4| B| six      |    |   |B |               |B
2| E| seven    |BC  |CE |  |               |BCE

1 个答案:

答案 0 :(得分:0)

试试这个......

Imports System.Data.OleDb

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim folder = "Path to your CSV folder (do not include the file name here)"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    Dim dt As New DataTable
    ' change Test.csv to your csv file name here
    Try
        Using Adp As New OleDbDataAdapter("Select * from [csv.csv]", CnStr)
            Adp.Fill(dt)
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    dt.Columns.Add("New Data", Type.GetType("System.String"))

    For Each DataRow In dt.AsEnumerable
        ' Get the data for new column
        Dim query = From r In dt.AsEnumerable() Where r.Field(Of Integer)("F1") = DataRow("F1") Select r.Field(Of String)("F2")
        ' Join it together ad put it in the DataTable
        DataRow("New Data") = String.Join("", query)
    Next

    DataGridView1.DataSource = dt
End Sub
End Class

将3列数据数据存储在名为&#39; csv.csv&#39;的文件中。并记得在上面的代码中设置csv文件的文件夹的路径(在Dim文件夹= .....)你需要有如下格式的数据......

1, A, one
2, B, two
3, A, three
2, C, four
1, D, five
4, B, six
2, E, seven

我尝试使用&#39; |&#39;但是现在没有时间让它工作,所以我用了一个&#39;&#39;分离csv文件中的值

无论如何,当你运行代码时,第4列会动态填充正确的代码....希望有帮助......

在手动将新行数据直接输入DataGridView1_CellEndEdit事件中的DataGridView1控件时,您也可以使用相同的逻辑更新第4列....

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

    For Each DataRow In dt.AsEnumerable
        ' Get the data for new column
        Dim query = From r In dt.AsEnumerable() Where r.Field(Of Integer)("F1") = DataRow("F1") Select r.Field(Of String)("F2")
        ' Join it together ad put it in the DataTable
        DataRow("New Data") = String.Join("", query)
        'dt.Rows(1)("New Data") = String.Join("", query)
    Next

End Sub

您还必须将DataTable的声明移动到Form Class的顶部,如此......

Public Class Form1

    Private dt As New DataTable