Skindeep2366
Skindeep2366

Reputation: 1559

Programmatically Changing a DataGridViewComboBox Display Member

This is a vb.net winforms application. I have a bound DataGridViewCombo box column inside a datagridview. Which allows the user to select 6 different transaction types. Cash transactions and Check Transactions both have the same ValueMember. What decides the 2 apart are if the Check Number column has a value or Not.. My problem is easy to see here. The ValueMember for both being the same makes using the DisplayMember to set the values for the ones that have a checkNumber required. This is just purely for the user experiance not for behind the scenes where it saves it as a payment. This is something like what I am needing of course its not correct because "Check Payment" is not valid as a ValueMember which requires an Integer.

       For i As Integer = 0 To FinancialDataGridView.RowCount - 1
        If Not IsNothing(FinancialDataGridView.Rows(i).Cells(2).Value) Then
            FinancialDataGridView.Rows(i).Cells(5).Value = "Check Payment"
        End If

    Next

Button Column Properties

But it gives an idea of the way I am thinking I could go about doing it.. Any ideas?

Upvotes: 1

Views: 1799

Answers (1)

GeorgeK
GeorgeK

Reputation: 647

Take a look at the below code, I hope it helps:

 For Each row As DataGridViewRow In FinancialDataGridView.Rows
        If Not IsNothing(row.Cells(2).Value) Then
            With CType(row.Cells(5), DataGridViewComboBoxCell)
                ' This gives you the ability to set the value member if you have the ID
                .ValueMember = 1
                ' This gives you the ability to set it by display memeber if you only have the string (Less safe)
                .DisplayMember = ("Your string")
            End With
        End If
    Next

You will see I have changed the for loop, by using for each you have access to the whole row while your in the loop.

And by ctyping the the cell to a DataGridViewComboBoxCell you get access to the display member and the value member.

Upvotes: 3

Related Questions