gpt4 book ai didi

c# - Combobox SelectedItem 的不确定响应

转载 作者:行者123 更新时间:2023-11-30 18:36:32 30 4
gpt4 key购买 nike

我在 winform 中工作并使用组合框。

我按这个顺序设置组合框的属性

combobox.DisplayMember 
combobox.ValueMember
combobox.DataSource

绑定(bind)代码是

   private void BindBranch()
{
BranchClass branch = new BranchClass();
branch.Active = true;
branch.Company = Startup.CompID;
if (branch.GetAll(out dtBranch, out result))
{
cmbBranch.DisplayMember = "br_name";
cmbBranch.ValueMember = "br_id";
cmbBranch.DataSource = dtBranch;
}
else
{
MessageBox.Show(result);
}
}

我在我的应用程序中多次使用组合框,我不知道我在哪里犯了错误

Sometime SelectedItem property returns exact value (int,string) based on the data type of "ValueMember" property, but sometime it returns "DataRowView", in which case an InvalidCastException is thrown.

我读到这是因为属性在组合框中设置的顺序。但现在不是这样了。

绑定(bind)代码为

enter image description here

调用代码是

private void BindEmployee() 
{
employee.Branch = Convert.ToInt32(cmbBranch.SelectedItem); // InvalidCastException thrown here
employee.Active = true;

var dt = new DataTable();
if (employee.GetEmployee(out dt, out result, false))
{
cmbEmployee.DisplayMember = "emp_name";
cmbEmployee.ValueMember = "emp_id";
cmbEmployee.DataSource = dt;
}
}

最佳答案

我假设您在寻找 br_id。你试过吗:

employeeBranch = ConvertToInt32(cmBranch.SelectedValue);

关于c# - Combobox SelectedItem 的不确定响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658493/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com