vb.net 如何通过 DataGridViewRow 中的列名引用 DataGridViewCell?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18101617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:34:52  来源:igfitidea点击:

How to reference a DataGridViewCell by column name in a DataGridViewRow?

.netvb.netwinformsdatagridview

提问by SuppaiKamo

I'm populating a DataGridViewas shown in the code below. However while each created row is associated with the parent DataGridView, in which the columns are created, I cannot reference cells in the row by column name.

我正在填充 a DataGridView,如下面的代码所示。但是,虽然每个创建的行都与创建列的 parent 相关联,但DataGridView我无法按列名引用行中的单元格。

Is there a way for me to reference by column name? I would rather avoid using the integer based index.

有没有办法让我按列名引用?我宁愿避免使用基于整数的索引。

EDIT: note that the columns of the DataGridView has been created and correctly named using the Visual Studio designer.

编辑:请注意,已使用 Visual Studio 设计器创建并正确命名了 DataGridView 的列。

Private Sub SetServiceOrders()
    Dim Row As DataGridViewRow = Nothing
    Dim RowValues As IServiceOrderDataGridViewRowValues = Nothing

    Me.ServiceOrdersDataGridView.Rows.Clear()
    If Not _ServiceOrders Is Nothing AndAlso _ServiceOrders.Count > 0 Then
        For Each ServiceOrder As ServiceOrder In _ServiceOrders.Values
            RowValues = ServiceOrder
            Row = New DataGridViewRow()
            Me.ServiceOrdersDataGridView.Rows.Add(Row)
            With Row
                'This Fails: "Specified argument was out of the range of valid values."
                .Cells("StatusImageColumn").Value = My.Resources.BulletGreen24
                .Cells("OrderDateColumn").Value = RowValues.Created
                .Cells("CreatedByColumn").Value = RowValues.OwnerName
            End With
            Me.ServiceOrdersDataGridView.Rows.Add()
        Next
    End If
End Sub

回答by Chris

You are not able to reference the DataGridViewCellby column name because the DataGridViewRowis not correctly created:

您无法引用DataGridViewCellby 列名称,因为DataGridViewRow未正确创建:

  Row = New DataGridViewRow() '=> new datagridview row with no knowledge about its DataGridView Parent
  Me.ServiceOrdersDataGridView.Rows.Add(Row) '

I would believe that the line: Me.ServiceOrdersDataGridView.Rows.Add(Row) would associate the row with the DataGridView.

我相信这一行:Me.ServiceOrdersDataGridView.Rows.Add(Row) 会将行与 DataGridView 相关联。

It seems that no. This should work instead:

似乎没有。这应该起作用:

 Private Sub SetServiceOrders()
        Dim Row As DataGridViewRow = Nothing
        Dim RowValues As IServiceOrderDataGridViewRowValues = Nothing
        Dim rowIndex As Integer 'index of the row

        Me.ServiceOrdersDataGridView.Rows.Clear()
        If Not _ServiceOrders Is Nothing AndAlso _ServiceOrders.Count > 0 Then
            For Each ServiceOrder As ServiceOrder In _ServiceOrders.Values
                RowValues = ServiceOrder

                '/////Create a new row and get its index/////
                rowIndex = Me.ServiceOrdersDataGridView.Rows.Add() 

               '//////Get a reference to the new row ///////
                Row = Me.ServiceOrdersDataGridView.Rows(rowIndex) 

                With Row
                    'This won't fail since the columns exist 
                    .Cells("StatusImageColumn").Value = My.Resources.BulletGreen24
                    .Cells("OrderDateColumn").Value = RowValues.Created
                    .Cells("CreatedByColumn").Value = RowValues.OwnerName
                End With

                '//// I think this line is not required 
                ' Me.ServiceOrdersDataGridView.Rows.Add() 
            Next
        End If
    End Sub

回答by James

Dim cellValue As String = DataGridView_Subjects.CurrentRow.DataBoundItem("columnName").ToString()

Dim cellValue As String = DataGridView_Subjects.CurrentRow.DataBoundItem("columnName").ToString()

回答by Khadim Ali

Since (it seems) you are not binding the DataGridView with the data object itself, you should not expect the column name as same as your data object properties.

由于(似乎)您没有将 DataGridView 与数据对象本身绑定,因此您不应期望列名与您的数据对象属性相同。

Try setting identical column names before filling data in the DataGridView manually.

在手动填充 DataGridView 中的数据之前尝试设置相同的列名。

dataGridView1.Columns(0).Name = "StatusImageColumn" ' and so on