《在DataGridView控件中加入ComboBox下拉列表框的实现.docx》由会员分享,可在线阅读,更多相关《在DataGridView控件中加入ComboBox下拉列表框的实现.docx(12页珍藏版)》请在三一办公上搜索。
1、在DataGridView控件中加入ComboBox下拉列表框的实现在DataGridView控件中加入ComboBox下拉列表框的实现 虽然在Visual Studio中 DataGridView控件的DataGridViewComboBoxColumn可以实现下拉列表框,但这样的列会在整列中都显示下拉列表框,不太 美观,而且还要用代码实现数据绑定。本文介绍一种只在当前编辑单元格中显示下拉列表框的方法,供大家参考。 首先新建一个Windows应用程序,将主窗体重命名为MainForm,在MainForm中加入一个DataGridView控件,命名为dgv_User。如下图所示: 打开窗体代码
2、窗口,在代码窗口中声明一个ComboBox的控件 / 定义下拉列表框 private ComboBox cmb_Temp = new ComboBox; 我们要绑定的下拉列表框的功能是选择性别,添加如下绑定性别下拉列表框的方法 / / 绑定性别下拉列表框 / private void BindSex DataTable dtSex = new DataTable; dtSex.Columns.Add(Value); dtSex.Columns.Add(Name); DataRow drSex; drSex = dtSex.NewRow; drSex0 = 1; drSex1 = 男; dtSe
3、x.Rows.Add(drSex); drSex = dtSex.NewRow; drSex0 = 0; drSex1 = 女; dtSex.Rows.Add(drSex); cmb_Temp.ValueMember = Value; cmb_Temp.DisplayMember = Name; cmb_Temp.DataSource = dtSex; cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList; 通常情况下我们都是从数据库中获取数据表,然后绑定到DataGridView中的,这里我们为了避免连接数据库,手中构造一个数据库表,代码如下
4、: private void BindData DataTable dtData = new DataTable; dtData.Columns.Add(ID); dtData.Columns.Add(Name); dtData.Columns.Add(Sex); DataRow drData; drData = dtData.NewRow; drData0 = 1; drData1 = 张三; drData2 = 1; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 2; drData1 = 李四; drData2 = 1
5、; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 3; drData1 = 王五; drData2 = 1; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 4; drData1 = 小芳; drData2 = 0; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 5; drData1 = 小娟; drData2 = 0; dtData.Rows.Add(drData); drData
6、 = dtData.NewRow; drData0 = 6; drData1 = 赵六; drData2 = 1; dtData.Rows.Add(drData); this.dgv_User.DataSource = dtData; 为窗体加载事件添加如下方法,其具体功能有详细说明: private void MainForm_Load(object sender, EventArgs e) / 绑定性别下拉列表框 BindSex; /绑定数据表 BindData; / 设置下拉列表框不可见 cmb_Temp.Visible = false; / 添加下拉列表框事件 cmb_Temp.Sel
7、ectedIndexChanged += new EventHandler(cmb_Temp_SelectedIndexChanged); / 将下拉列表框加入到DataGridView控件中 this.dgv_User.Controls.Add(cmb_Temp); 当用户选择的单元格移动到性别这一列时,我们要显示下拉列表框,添加如下事件 private void dgv_User_CurrentCellChanged(object sender, EventArgs e) try if (this.dgv_User.CurrentCell.ColumnIndex = 2) Rectangl
8、e rect = dgv_User.GetCellDisplayRectangle(dgv_User.CurrentCell.ColumnIndex, dgv_User.CurrentCell.RowIndex, false); string sexValue = dgv_User.CurrentCell.Value.ToString; if (sexValue = 1) cmb_Temp.Text = 男; else cmb_Temp.Text = 女; cmb_Temp.Left = rect.Left; cmb_Temp.Top = rect.Top; cmb_Temp.Width =
9、rect.Width; cmb_Temp.Height = rect.Height; cmb_Temp.Visible = true; else cmb_Temp.Visible = false; catch 当用户选择下拉列表框时改变DataGridView单元格的内容 private void cmb_Temp_SelectedIndexChanged(object sender, EventArgs e) if (ComboBox)sender).Text = 男) dgv_User.CurrentCell.Value = 男; dgv_User.CurrentCell.Tag = 1;
10、 else dgv_User.CurrentCell.Value = 女; dgv_User.CurrentCell.Tag = 0; 当滚动DataGridView或者改变DataGridView列宽时将下拉列表框设为不可见 private void dgv_User_Scroll(object sender, ScrollEventArgs e) this.cmb_Temp.Visible = false; private void dgv_User_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) this.
11、cmb_Temp.Visible = false; 绑定数据表后将性别列中的每一单元格的Value和Tag属性 private void dgv_User_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) for (int i = 0; i this.dgv_User.Rows.Count; i+) if (dgv_User.Rowsi.Cells2.Value != null & dgv_User.Rowsi.Cells2.ColumnIndex = 2) dgv_User.Rowsi.Cel
12、ls2.Tag = dgv_User.Rowsi.Cells2.Value.ToString; if (dgv_User.Rowsi.Cells2.Value.ToString = 1) dgv_User.Rowsi.Cells2.Value = 男; else if (dgv_User.Rowsi.Cells2.Value.ToString = 0) dgv_User.Rowsi.Cells2.Value = 女; 效果如下图所示: 完整的代码如下: using System; using System.Data; using System.Collections.Generic; usin
13、g System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyNameSpace public partial class MainForm : Form / 定义下拉列表框 private ComboBox cmb_Temp = new ComboBox; public MainForm InitializeComponent; / / 绑定性别下拉列表框 / private void BindSex DataTable dtSex = new
14、 DataTable; dtSex.Columns.Add(Value); dtSex.Columns.Add(Name); DataRow drSex; drSex = dtSex.NewRow; drSex0 = 1; drSex1 = 男; dtSex.Rows.Add(drSex); drSex = dtSex.NewRow; drSex0 = 0; drSex1 = 女; dtSex.Rows.Add(drSex); cmb_Temp.ValueMember = Value; cmb_Temp.DisplayMember = Name; cmb_Temp.DataSource = d
15、tSex; cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList; / / 为避免连接数据库,这里手工构造数据表,实际应用中应从数据库中获取 / private void BindData DataTable dtData = new DataTable; dtData.Columns.Add(ID); dtData.Columns.Add(Name); dtData.Columns.Add(Sex); DataRow drData; drData = dtData.NewRow; drData0 = 1; drData1 = 张三; drDa
16、ta2 = 1; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 2; drData1 = 李四; drData2 = 1; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 3; drData1 = 王五; drData2 = 1; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 4; drData1 = 小芳; drData2 = 0; dtData.Rows.Add(drData);
17、 drData = dtData.NewRow; drData0 = 5; drData1 = 小娟; drData2 = 0; dtData.Rows.Add(drData); drData = dtData.NewRow; drData0 = 6; drData1 = 赵六; drData2 = 1; dtData.Rows.Add(drData); this.dgv_User.DataSource = dtData; private void MainForm_Load(object sender, EventArgs e) / 绑定性别下拉列表框 BindSex; /绑定数据表 Bin
18、dData; / 设置下拉列表框不可见 cmb_Temp.Visible = false; / 添加下拉列表框事件 cmb_Temp.SelectedIndexChanged += new EventHandler(cmb_Temp_SelectedIndexChanged); / 将下拉列表框加入到DataGridView控件中 this.dgv_User.Controls.Add(cmb_Temp); / 当用户移动到性别这一列时单元格显示下拉列表框 private void dgv_User_CurrentCellChanged(object sender, EventArgs e) t
19、ry if (this.dgv_User.CurrentCell.ColumnIndex = 2) Rectangle rect = dgv_User.GetCellDisplayRectangle(dgv_User.CurrentCell.ColumnIndex, dgv_User.CurrentCell.RowIndex, false); string sexValue = dgv_User.CurrentCell.Value.ToString; if (sexValue = 1) cmb_Temp.Text = 男; else cmb_Temp.Text = 女; cmb_Temp.Le
20、ft = rect.Left; cmb_Temp.Top = rect.Top; cmb_Temp.Width = rect.Width; cmb_Temp.Height = rect.Height; cmb_Temp.Visible = true; else cmb_Temp.Visible = false; catch / 当用户选择下拉列表框时改变DataGridView单元格的内容 private void cmb_Temp_SelectedIndexChanged(object sender, EventArgs e) if (ComboBox)sender).Text = 男) d
21、gv_User.CurrentCell.Value = 男; dgv_User.CurrentCell.Tag = 1; else dgv_User.CurrentCell.Value = 女; dgv_User.CurrentCell.Tag = 0; / 滚动DataGridView时将下拉列表框设为不可见 private void dgv_User_Scroll(object sender, ScrollEventArgs e) this.cmb_Temp.Visible = false; / 改变DataGridView列宽时将下拉列表框设为不可见 private void dgv_U
22、ser_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) this.cmb_Temp.Visible = false; / 绑定数据表后将性别列中的每一单元格的Value和Tag属性 private void dgv_User_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) for (int i = 0; i this.dgv_User.Rows.Count; i+) if (dgv_User.Rowsi.Cells2.Value != null & dgv_User.Rowsi.Cells2.ColumnIndex = 2) dgv_User.Rowsi.Cells2.Tag = dgv_User.Rowsi.Cells2.Value.ToString; if (dgv_User.Rowsi.Cells2.Value.ToString = 1) dgv_User.Rowsi.Cells2.Value = 男; else if (dgv_User.Rowsi.Cells2.Value.ToString = 0) dgv_User.Rowsi.Cells2.Value = 女;