C# 如何在组合框中显示用户选择的值?

C# 如何在组合框中显示用户选择的值?,c#,winforms,C#,Winforms,我有一个DataGridView,它将员工ID、员工姓氏和员工名字存储到单元格单击时的字符串中 rEmpID = this.dtDataGridView.CurrentRow.Cells["REVIEWER"].Value.ToString(); rEmpFirst = this.dtDataGridView.CurrentRow.Cells["EMP_FIRSTNAME1"].Value.ToString(); rEmpLast = this.dtDataGridView.CurrentRo

我有一个DataGridView,它将员工ID、员工姓氏和员工名字存储到单元格单击时的字符串中

rEmpID = this.dtDataGridView.CurrentRow.Cells["REVIEWER"].Value.ToString(); 
rEmpFirst = this.dtDataGridView.CurrentRow.Cells["EMP_FIRSTNAME1"].Value.ToString();
rEmpLast = this.dtDataGridView.CurrentRow.Cells["EMP_LASTNAME1"].Value.ToString();
这工作正常,并将存储正确的值。在此之后,将创建一个新表单,其中包含一个公共的组合框。此组合框被数据绑定到sql server中包含员工id、员工姓氏和员工名的表中。组合框的显示成员是员工的姓氏,值成员是员工id。我希望能够获取rEmpID并让它更改组合框中的选定值。我试过了

pForm.assComboBox.SelectedItem = pForm.revComboBox.Items.IndexOf(rEmpID);
pForm.revComboBox.SelectedIndex = pForm.revComboBox.FindStringExact(rEmpLast);

我无法将此作为评论,因此以下是我使用的示例:

void Main()
{
    DataContext db = new DataContext(@"server=.\SQLexpress;trusted_connection=yes;database=Northwind");

    Table<Category> Categories = db.GetTable<Category>();
    Table<Product> Products = db.GetTable<Product>();

    Form f = new Form { Text="ComboBox ornek", Height=200, Width=500 };
    ComboBox cb1 = new ComboBox{ Left=10, Top=10, Width=450, Font=new Font("Courier New",8) };
    ComboBox cb2 = new ComboBox{ Left=10, Top=60, Width=450, Font=new Font("Courier New",8) };

    f.Controls.AddRange( new Control[] {cb1, cb2} );

    cb1.DataSource = Categories.OrderByDescending(c => c.CategoryName).ToList();
    cb1.ValueMember = "CategoryId";
    cb1.DisplayMember = "CategoryName";
    //cb1.SelectedIndex = -1;


    cb1.SelectedIndexChanged += (sender, args) => { 

    var selectedCategory = ((ComboBox)sender).SelectedItem as Category;
    cb2.DataSource = null;
    cb2.Items.Clear();
    if (selectedCategory != null)
    {
      cb2.DataSource = Products.Where (p => p.CategoryId == selectedCategory.CategoryId).ToList();
      cb2.DisplayMember = "ProductName";
      cb2.ValueMember = "ProductId";
    }
    };

    cb1.SelectedValue = 5;
    f.ShowDialog();
}


[Table(Name = "Categories")]
public class Category
{
    [Column]
    public int CategoryId { get; set; }
    [Column]
    public string CategoryName { get; set; }
    [Column]
    public string Description { get; set; }
}

[Table(Name = "Products")]
public class Product
{
    [Column]
    public int ProductId { get; set; }
    [Column]
    public string ProductName { get; set; }
    [Column]
    public int CategoryId { get; set; }
}
void Main()
{
DataContext db=new DataContext(@“服务器=。\SQLexpress;可信连接=是;数据库=北风”);
表Categories=db.GetTable();
Table Products=db.GetTable();
表格f=新表格{Text=“ComboBox ornek”,高度=200,宽度=500};
组合框cb1=新组合框{Left=10,Top=10,Width=450,Font=new Font(“Courier new”,8)};
组合框cb2=新组合框{Left=10,Top=60,Width=450,Font=new Font(“Courier new”,8)};
f、 AddRange(新控件[]{cb1,cb2});
cb1.DataSource=Categories.OrderByDescending(c=>c.CategoryName.ToList();
cb1.ValueMember=“CategoryId”;
cb1.DisplayMember=“CategoryName”;
//cb1.SelectedIndex=-1;
cb1.SelectedIndexChanged+=(发送方,参数)=>{
var selectedCategory=((组合框)sender)。选择editem作为类别;
cb2.DataSource=null;
cb2.Items.Clear();
如果(selectedCategory!=null)
{
cb2.DataSource=Products.Where(p=>p.CategoryId==selectedCategory.CategoryId.ToList();
cb2.DisplayMember=“ProductName”;
cb2.ValueMember=“ProductId”;
}
};
cb1.SelectedValue=5;
f、 ShowDialog();
}
[表(Name=“Categories”)]
公共类类别
{
[专栏]
public int CategoryId{get;set;}
[专栏]
公共字符串CategoryName{get;set;}
[专栏]
公共字符串说明{get;set;}
}
[表(Name=“产品”)]
公共类产品
{
[专栏]
public int ProductId{get;set;}
[专栏]
公共字符串ProductName{get;set;}
[专栏]
public int CategoryId{get;set;}
}

好了,伙计们,我想出了一个解决办法。这不好,但很管用。在带有组合框的表单上,我添加了一个公共标签。然后我将标签的文本设置为rEmpID值。在我设置的表单上

revComboBox.SelectedValue = label.Text;

同样不是很好,但对于将来遇到此问题并需要快速修复的任何人来说,这将完成此任务。

SelectedValue=remp感谢您的建议。不幸的是,它不起作用。嗯,它在我快速编写的示例代码中对我起作用。
pForm.revComboBox.SelectedIndex=pForm.revComboBox.Items.IndexOf(rEmpID)?是的,从我所读到的来看,它应该可以工作,但当我在代码中尝试它时,由于某种原因它就不能工作。也许我需要将我选择的值更改为int?现在rEmpID是一个存储在字符串中的数字。旁注:请尝试在DisplayMember和ValueMember属性之后设置数据源,以防止控件多次调用无效。@Person哦,为什么要这样做。我猜“员工id”不是一根绳子。Idk我只是在这一点上往墙上扔东西。这个问题一直困扰着我。谢谢大家的努力,但我需要从这个问题上休息一下,做点别的事情。