我必须用以下代码绑定组合框:
private void getCompanydata()
{
MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["RL_InventoryConnection"]);
if (con.State == ConnectionState.Closed)
con.Open();
MySqlCommand cmd = new MySqlCommand("select comp_id, concat(comp_name,'-', comp_add) as company from companymaster;", con);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
DataRow dr;
dr = dt.NewRow();
dr.ItemArray = new object[] {0, "--Select Delivery Location--" };
dt.Rows.InsertAt(dr, 0);
comboBox1.DisplayMember = "company";
comboBox1.ValueMember = "comp_id";
comboBox1.DataSource = dt;
}
在另一种方法中,我想访问与valueMember绑定的comp_id
。我正在尝试使用以下代码,但它不起作用:
private void SaveData()
{
string company = comboBox1.Text.ToString();
int companyid = Convert.ToInt32(comboBox1.SelectedValue);
}
您想尝试使用combobox1.SelectedValue
而不是combobox1.MemberValue
。
此处文档中的更多详细信息。。。https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.selectedvalue?view=windowsdesktop-6.0#系统窗口窗体列表控件选择值
类似的答案链接如下。。。https://stackoverflow.com/a/6901118/4462984