在 DataGridView 控件中设置行名称?

发布于 2024-12-09 15:43:08 字数 471 浏览 0 评论 0原文

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

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

VB.Net's DataGridView supports naming columns, but I was wondering if it could somehow do the same for rows, so that we could use something like this:

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

Do you confirm that it's not possible and we must use numeric indexes to refer to row numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2024-12-16 15:43:08

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"

There is no Name property on DataGridViewRow, so no, it's not possible.

You could always use a Dictionary to tie a name to a 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"
┾廆蒐ゝ 2024-12-16 15:43:08

存在一种使用 .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 & "' , "

exist a way to do this using .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 & "' , "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文