如何使用数据库值创建“自动完成”文本框

时间:2015-10-21 13:12:18

标签: c# winforms datagridview

我想使用文本框search_txt来过滤我的DataGridView值,以便在文本框中输入的任何字母自动完成并且我收到错误

这是创建的方法

void AutoCompleteText()
    {
        search_txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        search_txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();

        OleDbCommand command = new OleDbCommand();
        command.Connection = conDB;
        command.CommandText = "select CCSpn_CODE,CCLname,CCFname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCcurrentDt,CCImage from abaanaCC";
        OleDbDataReader myreader;
        try
        {
            conDB.Open();
            myreader = command.ExecuteReader();

            while (myreader.Read())
            {
               //Error Here!!!!! 
                string sName = myreader.GetString("CCLname")
                coll.Add(sName);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
        search_txt.AutoCompleteCustomSource = coll;

    }

这是加载DataGridView

的代码
 private void btnloaddata_Click(object sender, EventArgs e)
    {
        try
        {
            conDB.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = conDB;
            command.CommandText = "select CCSpn_CODE ,CCLname ,CCFname ,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian ,CCContact,CCcurrentDt,CCImage from abaanaCC";
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);

            dataGridView1.DataSource = dt;
           // this.dataGridView1.Sort(this.dataGridView1.Columns["CCLname"], ListSortDirection.Ascending);


        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to Load Data");
        }
        conDB.Close();

    }

1 个答案:

答案 0 :(得分:0)

这里是 AutoCompleteMethod

的解决方案
  void AutoCompleteTxtBox() 
    {
        txt_Search.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        txt_Search.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\ELIJAH\vs_Proj\Abaana\Abaana\abaana.mdb";
        conn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select CCSpn_CODE ,CCLname ,CCFname ,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian ,CCContact,CCcurrentDt,CCImage from abaanaCC";
        //cmd.CommandText = "select * from abaanaCC";
        OleDbDataReader myReader = cmd.ExecuteReader();
        while (myReader.Read())
        {//error was here
            string lname = myReader["CCLname"].ToString();
            coll.Add(lname);
       }
        txt_Search.AutoCompleteCustomSource = coll;
        conn.Close();
    }
相关问题