本文介绍了将数据从sql数据库显示到comboBox上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
单击搜索按钮时,请帮助我在组合框中显示数据
please help me display data into my combo box when a search button is clicked
private void lklSearch_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { conn.Open(); try { Personal person = new Personal(); if (person.searchpersonDetails(txtClientid.Text, txtSurname.Text)) { var _with3 = this; string mstatus = person.statusMarital.ToString(); switch (mstatus) { case "Single": this.cboMaritalstatus.Text = "Single"; break; case "Married": _with3.cboMaritalstatus.Text = "Married"; break; case "Widow(er)": _with3.cboMaritalstatus.Text = "Widow(er)"; break; case "Divorce": _with3.cboMaritalstatus.Text = "Divorce"; break; }在班上我有
and in the class i have
SqlDataReader dr = null; dr = cmd.ExecuteReader(); if (dr.Read()) { marital_status = dr["marital_status"].ToString();该程序将运行,但不会在组合框中显示数据.
the program run but do not display the data in the combo box
推荐答案您需要使用数据绑定.将您的CB数据源绑定到记录集,并设置DisplayMember和ValueMember. 阅读 .NET Windows表单中的数据绑定概念 [ ^ ]和 WPF中的数据绑定 [ ^ ] You need to use data bindings. Bind your CB DataSource to recordset and set the DisplayMember and ValueMember. Read Data binding concepts in .NET windows forms[^] and Data binding in WPF[^]
在数据读取中,按以下方式将项目添加到组合框中, Hi, Add items to your combo box in the below way in your data read, comboboxid.add.items(marital_status);
或获取数据集并将其绑定到您的组合框.例如,
or get a dataset and bind it to your combo box.like,
// Setup the combobox view and display-, value member comboboxid.DataSource = dsView; comboboxid.DisplayMember = "CompanyName"; comboboxid.ValueMember = "CustomerID";希望这会有所帮助. 另外,请查看此链接以获取数据绑定示例. www.akadia/services/dotnet_databinding.html [ ^ ]
Hope this helps. Also, check this link for data binding examples. www.akadia/services/dotnet_databinding.html[^]
尝试查看以下链接: msdn.microsoft/en-us/library/x8ybe6s2(VS .80).aspx [ ^ ] msdn.microsoft/en-us/library/w67sdsex.aspx [ ^ ] 问候 罗伯特 Hi, try to chceck following links: msdn.microsoft/en-us/library/x8ybe6s2(VS.80).aspx[^] msdn.microsoft/en-us/library/w67sdsex.aspx[^] Regards Robert
更多推荐
将数据从sql数据库显示到comboBox上
发布评论