tags:

views:

316

answers:

1

How to filter data in datagrid for example if u select the combo box in student number then input 1001 in the text field...all records in 1001 will appear in datagrid.....we are using sql server

private void button2_Click(object sender, EventArgs e)
{
    if (cbofilter.SelectedIndex == 0)
    {
        string sql;
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Server= " + Environment.MachineName.ToString() + @"\; Initial Catalog=TEST;Integrated Security = true";

        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds1 = new DataSet();
        ds1 = DBConn.getStudentDetails("sp_RetrieveSTUDNO");
        sql = "Select * from Test where STUDNO like '" + txtvalue.Text + "'";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.CommandType = CommandType.Text;
        da.SelectCommand = cmd;
        da.Fill(ds1);

        dbgStudentDetails.DataSource = ds1;
        dbgStudentDetails.DataMember = ds1.Tables[0].TableName;
        dbgStudentDetails.Refresh();
    }
    else if (cbofilter.SelectedIndex == 1)
    {
        //string sql;
        //SqlConnection conn = new SqlConnection();
        //conn.ConnectionString = "Server= " + Environment.MachineName.ToString() + @"\; Initial Catalog=TEST;Integrated Security = true";

        //SqlDataAdapter da = new SqlDataAdapter();
        //DataSet ds1 = new DataSet();
        //ds1 = DBConn.getStudentDetails("sp_RetrieveSTUDNO");
        //sql = "Select * from Test where Name like '" + txtvalue.Text + "'";
        //SqlCommand cmd = new SqlCommand(sql,conn);
        //cmd.CommandType = CommandType.Text;
        //da.SelectCommand = cmd;
        //da.Fill(ds1);

       // dbgStudentDetails.DataSource = ds1;
        //dbgStudentDetails.DataMember = ds1.Tables[0].TableName;
        //ds.Tables[0].DefaultView.RowFilter = "Studno = + txtvalue.text + "; 
        dbgStudentDetails.DataSource = ds.Tables[0];
        dbgStudentDetails.Refresh();
    }
}
A: 

It's difficult to answer pricisely to a vague question. I guess that you'll have to adapt your SQL query with a WHERE statement containing the user input.

If 'student number' is selected in the combo box, query like this (numbers starting with):

SELECT id, name, number FROM students WHERE number LIKE @search + '%'

If 'student name' is selected, use another query (names containing):

SELECT id, name, number FROM students WHERE name LIKE '%' + @search + '%'

Please explain in what sense C# is concerned.

Mart
aS I choosestudent number in combobox and enter the student number in the textfield as 1001 in student number...it will appear in the datagrid but the old data are still there..for example 1002 is still in the datagridheres the code in
malou17
Call the Clear() method on your DataSet before calling da.Fill(ds)
Mart