winform dategridview 自动完成。

1,先上图

 

代码如下。

 
ExpandedBlockStart.gif View Code
 1    private  AutoCompleteStringCollection autoCompleteSource  =   new  AutoCompleteStringCollection();
 2 
 3      private   void  dataGridView1_EditingControlShowing( object  sender, DataGridViewEditingControlShowingEventArgs e)
 4          {
 5              DataGridView dgv  =  (DataGridView)sender;
 6               if  (e.Control  is  TextBox)
 7              {
 8                  TextBox tb  =  (TextBox)e.Control;
 9                   if  (dgv.CurrentCell.OwningColumn.Name == " Name " )
10                  {
11                      tb.AutoCompleteMode  =  AutoCompleteMode.SuggestAppend;
12                      tb.AutoCompleteSource  =  AutoCompleteSource.CustomSource;
13                      tb.AutoCompleteCustomSource  =   this .autoCompleteSource;
14                  }
15                   else
16                  {
17                      tb.AutoCompleteMode  =  AutoCompleteMode.None;
18                  }
19              }
20          }
21 
22           private   void  dataGridView1_DataSourceChanged( object  sender, EventArgs e)
23          {
24              var dgv  =  (DataGridView)sender;
25               this .autoCompleteSource.Clear();
26               foreach  (DataGridViewRow r  in  dgv.Rows)
27              {
28                   string  val  =  r.Cells[ " Name " ].Value  as   string ;
29                   if  ( ! string .IsNullOrEmpty(val)  &&   ! this .autoCompleteSource.Contains(val))
30                  {
31                      autoCompleteSource.Add(val);
32                  }
33              }
34          }
35 
36           private   void  dataGridView1_CellValidating( object  sender, DataGridViewCellValidatingEventArgs e)
37          {
38 
39          }
40           // 添加新的成员
41           private   void  dataGridView1_CellValueChanged( object  sender, DataGridViewCellEventArgs e)
42          {
43               int  i = 0 ;
44               if  (e.RowIndex  ==   - 1 )
45              {
46                  i  =   0 ;
47              }
48               else
49              {
50                  i  =  e.RowIndex;
51              }
52              var dgv  =  (DataGridView)sender;
53               if  (dgv.Columns[e.ColumnIndex].Name  ==   " Name " )
54              {
55                   string  val  =  dgv[e.ColumnIndex, i].Value  as   string ;
56                   if  ( ! string .IsNullOrEmpty(val)  &&   ! this .autoCompleteSource.Contains(val))
57                  {
58                      autoCompleteSource.Add(val);
59                  }
60              }
61          }
 

 通过以上几个步骤就可以完成自动完成的实现了。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值