简体   繁体   English

以编程方式更改DataGridViewComboBox显示成员

[英]Programmatically Changing a DataGridViewComboBox Display Member

This is a vb.net winforms application. 这是一个vb.net winforms应用程序。 I have a bound DataGridViewCombo box column inside a datagridview. 我在datagridview中有一个绑定的DataGridViewCombo框列。 Which allows the user to select 6 different transaction types. 这允许用户选择6种不同的交易类型。 Cash transactions and Check Transactions both have the same ValueMember. 现金交易和支票交易都具有相同的ValueMember。 What decides the 2 apart are if the Check Number column has a value or Not.. My problem is easy to see here. 如果Check Number列有值或Not值,那么决定2分开的是什么。我的问题很容易在这里看到。 The ValueMember for both being the same makes using the DisplayMember to set the values for the ones that have a checkNumber required. 两者的ValueMember相同,使用DisplayMember设置需要checkNumber的值。 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. 这就像我需要的那样,当然它不正确,因为“支票付款”无效作为需要整数的ValueMember。

       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

按钮列属性

But it gives an idea of the way I am thinking I could go about doing it.. Any ideas? 但它给出了我认为我可以去做的方式的想法..任何想法?

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. 您将看到我已经更改了for循环,通过使用每个您可以在循环中访问整行。

And by ctyping the the cell to a DataGridViewComboBoxCell you get access to the display member and the value member. 通过将单元格ctyping到DataGridViewComboBoxCell,您可以访问显示成员和值成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM