C# ComboBox 使用示例

效果图

 

 form1.Designer.cs

 private System.Windows.Forms.ComboBox cmbInstallResult;
复制代码
// 
// cmbInstallResult
// 
this.cmbInstallResult.Cursor = System.Windows.Forms.Cursors.Hand;
this.cmbInstallResult.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.cmbInstallResult.Font = new System.Drawing.Font("微软雅黑", 10.28571F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.cmbInstallResult.FormattingEnabled = true;
this.cmbInstallResult.Location = new System.Drawing.Point(677, 137);
this.cmbInstallResult.Name = "cmbInstallResult";
this.cmbInstallResult.Size = new System.Drawing.Size(200, 31);
this.cmbInstallResult.TabIndex = 182;
复制代码

form1.cs

从数据库取值,赋到 ComboBox 下拉菜单中 两方法

方法一

string strSql_InstallWorker = "SELECT first_name FROM user where role_name='安装人员' " + " and title='" + Program.globalCurrentUserTitle + "'" + " and deleted=0  order by date_entered asc";
DataTable dtComboBox_InstallWorker = clsDBHelperMySQL.m_Query(strSql_InstallWorker);//执行sql 
this.cmbInstallWorker.Items.Add("--请选择--");
foreach (DataRow row in dtComboBox_InstallWorker.Rows)
{
     this.cmbInstallWorker.Items.Add(row[0]);
}
this.cmbInstallWorker.SelectedIndex = 0;

方法二

复制代码
private void setComboBoxOption(ref ComboBox objComboBox, string strItemName, string strDefaultOption)
{
            string strSql = "SELECT value_name FROM item_value where deleted=0 and value_name<>'' and item_name='" + strItemName + "' order by orderby+0 asc ";
            DataTable dtComboBox = clsDBHelperMySQL.m_Query(strSql);//执行sql 
            DataRow newRow = dtComboBox.NewRow();
            newRow[0] = strDefaultOption;// 给新行赋值  "--请选择--"
            dtComboBox.Rows.InsertAt(newRow, 0);//插入至第一行
            objComboBox.DataSource = dtComboBox;
            objComboBox.ValueMember = "value_name";//绑定
            objComboBox.DisplayMember = "value_name";
}
复制代码

调用方式

//安装情况---------------
this.setComboBoxOption(ref this.cmbInstallResult, "查询安装情况", "--请选择--");

组合成SQL查询条件时

string strInstallResult = this.cmbInstallResult.Text;
if (strInstallResult != "" && strInstallResult != "--请选择--")
{
    strWhere = strWhere + " and ( install_result ='" + strInstallResult + "' )";
}

 

posted @   海乐学习  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2023-06-14 easyui-datagrid 显示和隐藏
2023-06-14 将easyui-datagrid组件克隆至另一个 easyui-datagrid组件 表结构(列)及数据完全一样
2023-06-14 easyui-datagrid中的表结构(列)赋给另一个 easyui-datagrid组件
2023-06-14 easyui-datagrid 列表中实现 合计 功能
2023-06-14 web页面中导出Excel (方法四) 纯 js 前端将table中数据导出Excel 使用 js-xlsx
2023-06-14 web页面中导出Excel (方法三) 前端easyui-datagrid(分页)导出Excel 使用 datagrid-export.js
2023-06-14 web页面中导出Excel (方法二) 前端easyui-datagrid导出Excel 使用 datagrid-export.js
点击右上角即可分享
微信分享提示