//绑定 SelectedValue 是关键
//要绑定一个工程单位(ppm,cm,m/s,...),下拉列表
string strTab = "cfg_eng_unit";
string strSql = "SELECT eng_unit_id,eng_unit FROM cfg_eng_unit;";
DataSet ds_Unit = MySql.GetDataFromTable(strTab, strSql, ref _conn);
//GetDataFromTable是我取数据库集的一个方法
comUnit.DataSource = ds_Unit.Tables[0].DefaultView;
comUnit.DisplayMember = "eng_unit";
comUnit.ValueMember = "eng_unit_id";
string strTab1 = "cfg_point_current";
string strSql1= "SELECT a.eng_unit_id,b.eng_unit,a.* FROM cfg_point_current a "+
" left join cfg_eng_unit b on a.eng_unit_id=b.eng_unit_id;";
DataSet ds1 = MySql.GetDataFromTable(strTab1, strSql1, ref _conn);
DataTable dt = ds1.Tables[0];
comUnit.DataBindings.Add("SelectedValue", dt, "eng_unit_id");//关键句
本文介绍了一种从数据库中获取工程单位并将其绑定到界面下拉列表的方法。通过SQL查询从cfg_eng_unit表中获取所有工程单位及其ID,并使用GetDataFromTable方法获取数据集。之后,将数据集绑定到ComboBox控件上,显示工程单位名称,同时设置工程单位ID作为选定值。

8298

被折叠的 条评论
为什么被折叠?



