在DataGridView控件中将名称设置为行?

时间:2011-10-12 18:54:27

标签: vb.net datagridview rows

VB.Net的DataGridView支持命名列,但我想知道它是否能以某种方式对行做同样的事情,所以我们可以使用这样的东西:

With DataGridView1
    .Rows.Add(4)

    .Rows(0).Name = "Setting1"
    .Rows(1).Name = "Setting2"

    'Added two columns at design time
    .Columns("Key").Row("Setting1") = "Key1"
    .Columns("Value").Row("Setting1") = "Value1"
    .Columns("Key").Row("Setting2") = "Key2"
    .Columns("Value").Row("Setting2") = "Value2"
End With

您确认这是不可能的,我们必须使用数字索引来引用行号吗?

2 个答案:

答案 0 :(得分:1)

Name上没有DataGridViewRow属性,所以不,这是不可能的。

您始终可以使用Dictionary将姓名绑定到DataGridViewRow

Dim dictRows As New Dictionary(Of String, DataGridViewRow)

'Map names to corresponding rows
dictRows.Add("Setting1", DataGridView1.Rows(0))
dictRows.Add("Setting2", DataGridView1.Rows(1))

'Access rows with names, using dictionary
dictRows("Setting1").Cells("Key").Value = "Key1"
dictRows("Setting1").Cells("Value").Value = "Value1"

答案 1 :(得分:0)

使用.Value

存在一种方法
For Each dr As DataGridViewRow In DataGridView1.Rows
Dim hashrow As String = hst("SQLServer")
Dim cellvalue As String = dr.Cells(hashrow).Value.ToString
                        values = values & "'" & cellvalue & "' , "