C# 中ComboBox控件的数据绑定用法
效果图
代码
//下拉菜单 绑定--------------- string strSql = "SELECT value_name FROM item_value where deleted=0 and value_name<>'' and item_name='所属办事处' order by orderby+0 asc "; DataTable dtComboBox = clsDBHelperMySQL.m_Query(strSql);//执行sql DataRow newRow = dtComboBox.NewRow(); newRow[0] = "--请选择--";// 给新行赋值 dtComboBox.Rows.InsertAt(newRow, 0);//插入至第一行 this.cmbInstallOffice.DataSource = dtComboBox; this.cmbInstallOffice.ValueMember = "value_name";//绑定 this.cmbInstallOffice.DisplayMember = "value_name";
点查询时 判断是否为 --请选择-- 不是则加入为条件
string strInstallOffice = this.cmbInstallOffice.Text; //取值 if (strInstallOffice != "" && strInstallOffice != "--请选择--") { strWhere = strWhere + " and ( install_office ='" + strInstallOffice + "' )"; }