search in datagridview C# winfom

后端 未结 3 1593
小蘑菇
小蘑菇 2021-01-06 11:34

I want to put a search option in DataGridView, i.e., User types the string or int in TextBox and similar records the DataGridView shou

3条回答
  •  迷失自我
    2021-01-06 12:05

            private void txtsearchgroup_KeyUp(object sender, KeyEventArgs e) {
            SqlConnection objconnection = new SqlConnection(servername and ...);
            DataView Dv = new DataView();
            objcommand = new SqlCommand("select name from groupitems", objconnection);
            objdataadapter = new SqlDataAdapter();
            objdataadapter.SelectCommand = new SqlCommand();
            objdataadapter.SelectCommand = objcommand;
            objdataset = new DataSet();
            objconnection.Open();
            objdataadapter.Fill(objdataset);
            Dv.Table = objdataset.Tables[0];
            Dv.RowFilter = " name LIKE '%" + txtsearchgroup.Text + "%'";
            dataGridView1.DataSource = Dv;
            objconnection.Close(); }
    
    • txtsearchgroup : name of textbox for search word in datagridview1 and
    • txtsearchgroup_KeyUp : event of keyup for search and filter word in datagridview1 and
    • select name from groupitems : name is field for groupitems table and
    • Dv.Table = objdataset.Tables[0] : zero (0) is first table in dataset

提交回复
热议问题