求和列标题和列值

时间:2013-02-21 03:56:42

标签: vb.net winforms datagridview

如何在datagridview VB.net中对textbox列标题和列值进行求和按datagridview列使用的值(For& Next)? 示例

datagridview:之前

          column : ID        address        

                   1         street1 

                   2         street2

                   3         street3

textbox:之后

                  ID = 1 / address = street1 

                  ID = 2 / address = street2 

                  ID = 3 / address = street3 

1 个答案:

答案 0 :(得分:0)

这一小段代码会将值添加到列表中,您可以从中填充文本框。它假定未知数量的列,dgv是您的DataGridView:

    Dim txtBoxList As New List(Of String)
    Dim strLine As String
    For Each dgvRow As DataGridViewRow In dgv.Rows
        For i As Integer = 0 To dgv.Columns.Count - 1
            strLine = strLine & String.Format("{0} = {1}", dgv.Columns(i).HeaderText.ToString, dgvRow.Cells(i).tostring)
            If i < dgv.Columns.Count - 1 Then
                strLine = strLine & " / "
            End If
        Next
        txtBoxList.Add(strLine)
        strLine = String.Empty
    Next