Datagridview中的自动完成自定义控件 [英] Autocomplete custom control in datagridview

查看:84
本文介绍了Datagridview中的自动完成自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用了datagrid,并通过column集合添加了列
现在,当用户在单元格中输入数据,然后在单元格离开时,它将检查是否与dat id有关的项存在或不存在

现在我希望它在用户写入项目代码为自动完成的单元格中成为一个单元格
我如何使数据自动完成

Hi

I am used a datagrid and add columns through columns collection
now when user enter data in a cell then on cell leave it will check wheather the item concerned with dat id is exist or not

now wat i want it dat a cell in which user write item code is autocomplet
how i make dat autocomplete

推荐答案

public partial class Form1 : Form
{
    private AutoCompleteStringCollection sc0 = null; // Column 0
    private AutoCompleteStringCollection sc1 = null; // Column 1

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Load Data
        sc0 = new AutoCompleteStringCollection() { "00", "000", "00000" };
        sc1 = new AutoCompleteStringCollection() { "11", "111", "11111" };
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is DataGridViewTextBoxEditingControl)
        {
            var tec = (e.Control as DataGridViewTextBoxEditingControl);
            tec.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tec.AutoCompleteSource = AutoCompleteSource.CustomSource;

            switch (dataGridView1.CurrentCell.ColumnIndex)
            {
                case 0:
                    tec.AutoCompleteCustomSource = sc1;
                    break;
                case 1:
                    tec.AutoCompleteCustomSource = sc1;
                    break;
            }
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        // Save

        // Not Contains
        string s = "";
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            s = dataGridView1[0, i].Value.ToString();
            if (!sc0.Contains(s))
            {
                sc0.Add(s);
            }

            s = dataGridView1[1, i].Value.ToString();
            if (!sc1.Contains(s))
            {
                sc1.Add(s);
            }
        }
    }
}


使用DataGridview的CellValueChanged或CellLeave事件来检入数据库.
您可以编写类似..
的代码 将事件添加到datagridview
use CellValueChanged or CellLeave event of DataGridview to check into the DB..

you can write code like..
add event to datagridview
Datagridview1.CellValueChanged += new DataGridViewCellEventHandler(Datagridview1_CellLeave);


然后用您在事件中提到的方法编写您的逻辑..


then Write your logic in the method you mentioned in the event..

void Datagridview1_CellLeave(object sender, DataGridViewCellEventArgs e)
       {
           // your logic here

       }


private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           //if (dataGridView1.CurrentCell.ColumnIndex ==1)
           //{
               if (e.Control is DataGridViewTextBoxEditingControl)
               {
                   DataGridViewTextBoxEditingControl te =
                   (DataGridViewTextBoxEditingControl)e.Control;
                   te.Name = "items";
                   te.AutoCompleteMode = AutoCompleteMode.Suggest;
                   te.AutoCompleteSource = AutoCompleteSource.CustomSource;
                   SqlConnection conn1 = new SqlConnection(gb.autocreateconn());
                   conn1.Open();
                   string comand2 = @"select xitems.it_id from xitems left join  inv1det on inv1det.ln_itemid=xitems.it_id";
                   SqlDataAdapter adatter2 = new SqlDataAdapter(comand2, conn1);
                   DataSet dataset2 = new DataSet();
                   adatter2.Fill(dataset2, "xitems");
                   string[] itemsg = new string[50];
                   int col = dataGridView1.CurrentCell.ColumnIndex;
                   switch(col){
                       case 0:
                           break;
                       case 1:
                   for (int i = 0; i < dataset2.Tables["xitems"].Rows.Count; i++)
                   {
                       itemsg[i] = dataset2.Tables["xitems"].Rows[i][0].ToString();
                       te.AutoCompleteCustomSource.AddRange(new string[] { itemsg[i] });
                   }
                   break;
                       case 3:
                   break;
                       case 4:
                   break;
                       case 5:
                   break;
                       default:
                    break;
                   }
               }

           //}
       }


这篇关于Datagridview中的自动完成自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆