簡體   English   中英

以編程方式更改DataGridViewComboBox顯示成員

[英]Programmatically Changing a DataGridViewComboBox Display Member

這是一個vb.net winforms應用程序。 我在datagridview中有一個綁定的DataGridViewCombo框列。 這允許用戶選擇6種不同的交易類型。 現金交易和支票交易都具有相同的ValueMember。 如果Check Number列有值或Not值,那么決定2分開的是什么。我的問題很容易在這里看到。 兩者的ValueMember相同,使用DisplayMember設置需要checkNumber的值。 這純粹是為了用戶體驗而不是在幕后將其保存為付款。 這就像我需要的那樣,當然它不正確,因為“支票付款”無效作為需要整數的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

按鈕列屬性

但它給出了我認為我可以去做的方式的想法..任何想法?

看看下面的代碼,我希望它有所幫助:

 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

您將看到我已經更改了for循環,通過使用每個您可以在循環中訪問整行。

通過將單元格ctyping到DataGridViewComboBoxCell,您可以訪問顯示成員和值成員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM