zoukankan      html  css  js  c++  java
  • 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;
                        
                    }
    
                }
  • 相关阅读:
    计算图像数据集RGB各通道的均值和方差
    多个数组的排列组合
    n个数中选取m个数,并全排列
    设计模式——代理模式
    简易的工厂模式
    多态
    final关键字特点
    this和super的区别
    重载与重写的区别
    Linux下安装MongoDB
  • 原文地址:https://www.cnblogs.com/xiaowie/p/8651702.html
Copyright © 2011-2022 走看看