将数据从sql数据库显示到comboBox上

编程入门 行业动态 更新时间:2024-10-24 22:19:49
本文介绍了将数据从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上

本文发布于:2023-11-10 01:05:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据   sql   comboBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!