首页 > 代码库 > DataGridView单元格内容自动匹配下拉显示

DataGridView单元格内容自动匹配下拉显示

页面显示数据使用的控件是ComponentFactory.Krypton.Toolkit中的KryptonDataGridView控件。在指定“商品”单元格中需要根据用户输入内容自动匹配数据库中商品信息,并且单元格处于编辑模式时显示一个查询图标的按钮,点击该按钮也将显示数据库中所有商品信息。KryptonDataGridView显示控件此处命名为kDGVIndentDetail;用于下拉显示匹配内容的DataGridView命名为dgv;1、建立一个DataGridView类型的页面变量用于KryptonDataGridView单元格下拉显示匹配结果1 //新建变量用于下拉显示的数据控件2  3 DataGridView dgv = new DataGridView();4 5  //保存当前编辑单元格的坐标6  7 private int _colindex;//列索引8  9 private int _rowindex;//行索引10    2、页面Load事件中设置dgv初始属性和添加一个单元格单击事件,提取为方法:代码 1 //设置下拉显示控件的属性2  3 private void _SetDGVDrop()4 5 {6 7 //初始为不可见并且只读8  9 dgv.Visible = false;10 11 dgv.ReadOnly = true;12 13 dgv.Height = 100;14 15 //单元格选择模式为正行选择16  17 dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;18 19 dgv.MultiSelect = false; //只用单行选择20  21 dgv.ColumnHeadersVisible = false; //隐藏列头22 23 //设置列宽24  25 dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;26 27 dgv.BackgroundColor = Color.FromName("window");28 29 dgv.RowHeadersVisible = false; //隐藏行头30  31 dgv.ScrollBars = ScrollBars.Vertical;//滚动条模式32  33 dgv.AllowUserToAddRows = false; //禁止添加新行34  35 dgv.AllowUserToDeleteRows = false;36 37 dgv.AllowUserToOrderColumns = false;38 39 dgv.AllowUserToResizeColumns = false;40 41 dgv.AllowUserToResizeRows = false;42 43 dgv.BackgroundColor = Color.White;44 45 //设置单元格边框样式46  47 dgv.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;48 49 dgv.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;50 51 dgv.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;52 53 dgv.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;54 55 //添加单元格鼠标单击事件56  57 dgv.CellMouseClick += new DataGridViewCellMouseEventHandler(dgv_CellMouseClick);58 59 //向明细单显示控件中加入该下拉控件60  61 kDGVIndentDetail.Controls.Add(dgv);62 63 }64 65     dgv的单元格单击事件:3、数据显示控件kDGVIndentDetail的商品列(colProId)类型为:KryptonDataGridViewTextBoxCell,在列的ButtonSpecs属性中添加一个buttonSpecAny名称为buttonSpecAny4,设置buttonSpecAny4的图片:  4、buttonSpecAny4的单击事件中显示下拉显示控件并调用控件的绑定事件:  当单元格为编辑状态时,buttonSpecAny4按钮将显示,点击该按钮下拉控件显示所有数据,点击控件的某一列,将把指定的值赋给当前编辑的单元格(这里只取选中行[“aa”]列的值)5、dgv的绑定方法:(为避免连接数据库,这里使用手工填充数据的方法)代码 1 //绑定下拉显示控件2  3 private void BindDGV(string str)4 5 {6 7 //填充数据源8  9 DataTable dt = new DataTable();10 11 dt.Columns.Add("aa");12 13 dt.Columns.Add("bb");14 15 dt.Columns.Add("cc");16 17 dt.Columns.Add("ee");18 19 DataRow dr = null;20 21 for (int i = 0; i < 5; i++)22 23 {24 25 dr = dt.NewRow();26 27 dr["aa"] = str + "aa" + i;28 29 dr["bb"] = str + "bb" + i;30 31 dr["cc"] = str + "cc" + i;32 33 dr["ee"] = str + "ee" + i;34 35 dt.Rows.Add(dr);36 37 }38 39  //绑定数据源 40  41 dgv.DataSource = dt;42 43 }44 45      6、现在实现在单元格直接输入内容时自动配备显示数据库中的内容,在kDGVIndentDetail显示控件的EditingControlShowing事件中处理:  TextChanged事件中根据用户数据内容动态匹配代码 1 void kcell_TextChanged(object sender, EventArgs e)2 3 {4 5 KryptonDataGridViewTextBoxEditingControl kdgvtbox = (KryptonDataGridViewTextBoxEditingControl)sender;6 7 //MessageBox.Show(kdgvtbox.Text);8  9 if (kDGVIndentDetail.Columns[kDGVIndentDetail.CurrentCell.ColumnIndex].Name == "colProId")10 11 {12 13 BindDGV(kdgvtbox.Text);14 15 _colindex = kDGVIndentDetail.CurrentCell.ColumnIndex;16 17 _rowindex = kDGVIndentDetail.CurrentCell.RowIndex;18 19 Rectangle rect = kDGVIndentDetail.GetCellDisplayRectangle(_colindex, _rowindex, false);20 21 //设置显示控件的位置22  23 dgv.Left = rect.Left;24 25 dgv.Top = rect.Top + kdgvtbox.Size.Height;26 27 dgv.Visible = true;28 29 }30 31 32 33 }34 35    7、滚动kDGVIndentDetail和改变列宽时,隐藏下拉显示控件代码 1 private void kDGVIndentDetail_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)2 3 {4 5 //隐藏下拉显示控件6  7 dgv.Visible = false;8 9 }10 11 12 13 private void kDGVIndentDetail_Scroll(object sender, ScrollEventArgs e)14 15 {16 17 //隐藏下拉显示控件18  19 dgv.Visible = false;20 21 }22 23    暂且写到这里,还需要在使用中调试修改,下面是效果图:单击单元格进入编辑模式时,查询按钮显示(放大镜) 输入内容时显示匹配内容: 点击放大镜显示所有内容: 代码 1 //单元格启用编辑时2  3 private void kDGVIndentDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)4 5 {6 7 //MessageBox.Show(""+sender.GetType().ToString());8  9 KryptonDataGridView kdgv = (KryptonDataGridView)sender;10 11 if (e.Control.GetType().Equals(typeof(KryptonDataGridViewTextBoxEditingControl))) //cell类型12  13 {14 15 KryptonDataGridViewTextBoxEditingControl kcell = e.Control as KryptonDataGridViewTextBoxEditingControl;16 17 //MessageBox.Show(kcell.TextBox.Name);18 19 //获取列名20 21 //MessageBox.Show(kdgv.Columns[kdgv.CurrentCell.ColumnIndex].Name);22  23 if (kdgv.Columns[kdgv.CurrentCell.ColumnIndex].Name == "colProId")//此列为显示商品代码的列24  25 { 26 27 //添加按键事件28  29 kcell.TextChanged += new EventHandler(kcell_TextChanged);30 31 }32 33 }34 35 }36 37     代码 1 private void buttonSpecAny4_Click(object sender, EventArgs e)2 3 {4 5 //点击可查询6 7 //MessageBox.Show("点击查询:"+buttonSpecAny4.Owner.GetType());8  9 KryptonDataGridViewTextBoxCell kdcell = (KryptonDataGridViewTextBoxCell)buttonSpecAny4.Owner;10 11 BindDGV("");12 13 Rectangle rect = kDGVIndentDetail.GetCellDisplayRectangle(kdcell.ColumnIndex, kdcell.RowIndex, false);14 15 _colindex = kdcell.ColumnIndex;16 17 _rowindex = kdcell.RowIndex;18 19 //设置显示控件的位置20  21 dgv.Left = rect.Left;22 23 dgv.Top = rect.Top + kdcell.Size.Height;24 25 26 27 dgv.Visible = true;28 29 }30 31    代码 1 //下拉显示控件单元格双击事件,获得选择值并赋值给当前单元格2  3 void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)4 5 {6 7 DataGridView dgv = (DataGridView)sender;8 9 KryptonDataGridView kdgv = (KryptonDataGridView)dgv.Parent;10 11 kdgv[_colindex,_rowindex].Value = http://www.mamicode.com/dgv.CurrentRow.Cells["aa"].Value;12 13 //隐藏 KryptonDataGridView14  15 dgv.Visible = false;16 17 //结束编辑,以显示选择结果值18  19 kDGVIndentDetail.EndEdit();20 21 }

 

DataGridView单元格内容自动匹配下拉显示