C#下拉框ComboBox动态绑定数据库源
2018-03-06 14:36
756 查看
下拉框动态加载数据库某字典表里的字段作为item展示。
思路是:1数据库访问辅助类 SQLhelper; 用途:把从数据库表中查询到字段加载到datatable里面备用;2一个combox辅助类 SetComboData;用途:把datatable里的各个row加载到combox的item里3最后在包含Combobox的界面实现:1先准备出来 SQLhelper
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace MedicalRecord.Class
{
internal class SetCommData
{
private CheckConfig checkConfig = new CheckConfig();
private SQLhelper sqLhelper = new SQLhelper();
public void SetCommItem(ComboBox comboBox, string tablename)
{
List<string> contenList = new List<string>();
string connectionstr = checkConfig.GetAppSettings();
//sql大家要根据实际情况,可能SetCommItem整个方法都要重新改造
string sqlstr = string.Format("select name from {0} where name <>'' and name is not null ", tablename);
DataTable dt = sqLhelper.GetDataTableValue(connectionstr, sqlstr);
//加载到combobox
comboBox.Items.Clear();
comboBox.Items.Add("");
for (int i = 0; i < dt.Rows.Count; i++)
{
comboBox.Items.Add(dt.Rows[i]["name"]);
}
comboBox.SelectedText = "";
}
}
}
3调用:
思路是:1数据库访问辅助类 SQLhelper; 用途:把从数据库表中查询到字段加载到datatable里面备用;2一个combox辅助类 SetComboData;用途:把datatable里的各个row加载到combox的item里3最后在包含Combobox的界面实现:1先准备出来 SQLhelper
internal class SQLhelper { public static SqlConnection conn; //打开数据库连接 private static void OpenConn(string connectionString) { conn = new SqlConnection(connectionString); if (conn.State.ToString().ToLower() != "open") { conn.Open(); } } //关闭数据库连接 public static void CloneConn() { if (conn.State.ToString().ToLower() != "open") { conn.Close(); conn.Dispose(); } } // DataTable public DataTable GetDataTableValue(string connectionString, string sql) { OpenConn(connectionString); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, conn); DataTable dataTable = new DataTable(); sqlDataAdapter.Fill(dataTable); CloneConn(); return dataTable; } } }2准备using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace MedicalRecord.Class
{
internal class SetCommData
{
private CheckConfig checkConfig = new CheckConfig();
private SQLhelper sqLhelper = new SQLhelper();
public void SetCommItem(ComboBox comboBox, string tablename)
{
List<string> contenList = new List<string>();
string connectionstr = checkConfig.GetAppSettings();
//sql大家要根据实际情况,可能SetCommItem整个方法都要重新改造
string sqlstr = string.Format("select name from {0} where name <>'' and name is not null ", tablename);
DataTable dt = sqLhelper.GetDataTableValue(connectionstr, sqlstr);
//加载到combobox
comboBox.Items.Clear();
comboBox.Items.Add("");
for (int i = 0; i < dt.Rows.Count; i++)
{
comboBox.Items.Add(dt.Rows[i]["name"]);
}
comboBox.SelectedText = "";
}
}
}
3调用:
private void LoadCommonData() { setCommData.SetCommItem(xb, "mis.dbo.DICT_ADMISSION"); setCommData.SetCommItem(ryks, "mis.dbo.DICT_ADMISSION"); }效果截图:
相关文章推荐
- 转:c# winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- c# winform comboBox动态绑定数据
- C# winform combobox 在绑定数据之后插入一项选择项
- [★]ComboBox 经验总结[动态绑定数据之后,静态添加一条数据]
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- 从后台绑定数据到ligerui 的comboBox下拉框组件
- easyui datagrid动态修改editor时动态绑定combobox的数据
- # winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- C# 一个通过.xsd文件动态绑定数据的水晶报表
- C#中为啥俺地ComboBox控件在数据绑定时加载速度很慢
- C#实现的多列数据绑定组合框控件MultiColumnComboBoxEx
- c# combobox 数据绑定
- C# comboBox1绑定数据集
- (C#)WPF类似下拉框的自动完成源,数据是从数据库查询得出动态数据
- C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值
- datatable绑定comboBox显示数据[C#]
- DevExPress控件中ComboBoxEdit动态绑定数据
- C# Winform Combobox手动绑定数据
- C# ComboBox(DropDownList)数据绑定再添加一项
- c# winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据