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
您确认这是不可能的吗?我们必须使用数字索引来引用行号?
DataGridViewRow
上没有Name
属性,所以不可能。
您可以始终使用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"
有一种使用.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 & "' , "