将数据从sql数据库显示到comboBox上 [英] display data from sql database onto a comboBox

查看:641
本文介绍了将数据从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";



希望这会有所帮助.

另外,请查看此链接以获取数据绑定示例.
http://www.akadia.com/services/dotnet_databinding.html [



Hope this helps.

Also, check this link for data binding examples.
http://www.akadia.com/services/dotnet_databinding.html[^]



尝试查看以下链接:
http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS .80).aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/w67sdsex.aspx [ ^ ]
问候
罗伯特
Hi,
try to chceck following links:
http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS.80).aspx[^]
http://msdn.microsoft.com/en-us/library/w67sdsex.aspx[^]
Regards
Robert


这篇关于将数据从sql数据库显示到comboBox上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆