SuppaiKamo
SuppaiKamo

Reputation: 285

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

I'm populating a DataGridView as 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.

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.

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

Upvotes: 1

Views: 20842

Answers (3)

James
James

Reputation: 135

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

Upvotes: 1

Chris
Chris

Reputation: 8647

You are not able to reference the DataGridViewCell by column name because the DataGridViewRow is not correctly created:

  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.

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

Upvotes: 3

Khadim Ali
Khadim Ali

Reputation: 2598

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.

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

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

Upvotes: 0

Related Questions