DataGridView 单元格自动填充
在DataGridView单元格中,当输入指定字符时,自动完成填充。
通过 TextBox实现
AutoCompleteMode
AutoCompleteMode.Suggest;
AutoCompleteSource
AutoCompleteSource.customSource;
namespace DataGridView单元格自动填充 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string constr = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1"; SqlConnection mycon = new SqlConnection(constr); try { mycon.Open(); DataTable mytb = new DataTable(); SqlDataAdapter mydpt = new SqlDataAdapter("select * from book",mycon); mydpt.Fill(mytb); dataGridView1.DataSource = mytb; mycon.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } //添加编辑控件显示事件 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { //定义字符串来确定你要自动填充那列 string titletext=dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText; //判断title和“shuoming”是否相等也可以用等号== if (titletext.Equals("shuoming")) { //控件textbox = 编辑控件显示事件 别名为textbox TextBox autotext = e.Control as TextBox; if (autotext!=null) { autotext.AutoCompleteMode = AutoCompleteMode.Suggest; autotext.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection datacoll = new AutoCompleteStringCollection(); datacoll.Add("监控专用"); datacoll.Add("共享专用"); autotext.AutoCompleteCustomSource= datacoll; } }