views:

115

answers:

1

HI all, I have an issue where i have to search rows in a datagrid view based on the key value given explicitly through textbox. This key value should be of any column in the datagridview. But unforutunately it works only for one column (in the below code column used is b)


            string ss;
            ss = textBox1.Text;
            SqlConnection con = new SqlConnection("data source=localhost;integrated security=true;initial catalog=da");
            da = new SqlDataAdapter("select * from me where a='"+ss+"' ", con);
            da.Fill(ds, "me");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt;
            DataView dv = ds.Tables[0].DefaultView;

//where b is a column in the table me. I need this function to work for multiple columns dv.RowFilter = "b='" + ss + "'"; ds.Tables.Clear(); ds.Tables.Add(dv.ToTable()); ds.AcceptChanges(); da.Update(dt);


Datagrid View before selecting

A: 
ss = textBox1.Text;
SqlConnection con = new SqlConnection("data source=localhost;
              integrated security=true;initial catalog=da");
da = new SqlDataAdapter("select * from me", con);
da.Fill(ds, "me");

DataView dv = ds.Tables[0].DefaultView;

dataGridView1.DataSource = dv;
dv.RowFilter = "col1='" + ss + "' and col2>=10";

EDIT:

Consider the following example,

    DataTable dt = new DataTable();
    DataView dv;
    private void Form1_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("No", typeof(int));
        dt.Columns.Add("Name");
        dt.Rows.Add(10, "abc");
        dt.Rows.Add(11, "pqr");
        dt.Rows.Add(12, "efg");
        dv = dt.DefaultView;
        dataGridView1.DataSource = dv;
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int no;
        int.TryParse(textBox1.Text, out no);
        dv.RowFilter = "No=" + no + " or Name like '%" + textBox1.Text + "%'";
    }
adatapost
thanks but if i use many columns
ush
i am bounding database with datagrid in c#,i want to write coding to find any value in a datagridview by giving that value in textbox,for that i used rowfilter but it works for single column this is my question
ush
ok thank u so much i got it
ush