过匹配DisplayMember值来查找组合框的ValueMem

过匹配DisplayMember值来查找组合框的ValueMem

本文介绍了如何通过匹配DisplayMember值来查找组合框的ValueMember值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为cboProductName的组合框,其显示成员是ProductName,值成员是ProductNo

,我有一个显示productNames的DataGridView。



当我点击DataGridView时,我想在组合框中显示单击的ProductName并将SelectedValue设置为其ProductNo。



以下代码是我尝试过的我知道这不对。

I have a combo Box named cboProductName which display member is ProductName and value member is ProductNo
and i have a DataGridView showing productNames.

When i click on the DataGridView, I want to show the clicked ProductName in combo Box and set SelectedValue to its ProductNo.

The following code is what I tried and I know it isn't right.

void gvProducts_CellClick(object sender, DataGridViewCellEventArgs e)
{
    cboProductName.SelectedItem = gvProducts["gv_col_ProductName", e.RowIndex].Value;
    txtQuantity.Text = gvProducts["gv_col_Quantity",e.RowIndex].Value.ToString();
    txtPrice.Text = gvProducts["gv_col_Price",e.RowIndex].Value.ToString();
}

推荐答案

cboProductName.SelectedIndex=cboProductName.Items.IndexOf(cboProductName.Items.FindByValue(gvProducts["gv_col_ProductName", e.RowIndex].Value));


For index = 0 To cmbDistibuter.Items.Count - 1
        cmbDistibuter.SelectedIndex = index
        Dim dr As DataRowView = TryCast(Me.BindingContext(cmbDistibuter.DataSource).Current, DataRowView)
        If dr(1).ToString() = "your value" Then
            Exit For
        End If
    Next


这篇关于如何通过匹配DisplayMember值来查找组合框的ValueMember值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:04