我想在列使用文本框中搜索名称的任何部分

时间:2016-12-21 06:15:59

标签: c#

我使用此代码

private void Autocomplete1()
{
        try
        {

            con = new SqlConnection(cs.DBConn);
            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT k1 FROM ork ", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds, "k1");

            AutoCompleteStringCollection col = new AutoCompleteStringCollection();
            int i = 0;

            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                col.Add(ds.Tables[0].Rows[i]["k1"].ToString());
            }

            textBox7.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox7.AutoCompleteCustomSource = col;
            textBox7.AutoCompleteMode = AutoCompleteMode.Suggest;

            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}

我想在列使用textbox7

中搜索名称的任何部分

2 个答案:

答案 0 :(得分:0)

private void LoadData()
        {
            using (SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=MyDB;Persist Security Info=True;User ID=sa; Password =password123;"))
            {
                con.Open();
                AutoCompleteStringCollection citycollection = new AutoCompleteStringCollection();
                string Qry = "select * from Cities";
                SqlCommand cmd = new SqlCommand(Qry, con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        citycollection.Add(dr["Name"].ToString());
                    }

                }
                textBox1.AutoCompleteMode = AutoCompleteMode.Append;
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox1.AutoCompleteCustomSource = citycollection;
            }
        }

答案 1 :(得分:0)

我想要类型&gt;&gt;&gt;&gt;说单词伦敦&gt;&gt;&gt; TYPE L&gt;&gt;单词London出现&lt;&lt;

当类型(o)或(n)没有出现时

如何更新这些代码来执行这些

private void Autocomplete1()
    {
            try
            {

                con = new SqlConnection(cs.DBConn);
                con.Open();

                SqlCommand cmd = new SqlCommand("SELECT k1 FROM ork ", con);
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds, "k1");

                AutoCompleteStringCollection col = new AutoCompleteStringCollection();
                int i = 0;

                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    col.Add(ds.Tables[0].Rows[i]["k1"].ToString());
                }

                textBox7.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox7.AutoCompleteCustomSource = col;
                textBox7.AutoCompleteMode = AutoCompleteMode.Suggest;

                con.Close();
            }
         catch (Exception ex)
         {
          MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
    }