datagrid filter in c# using sql server

Posted by malou17 on Stack Overflow See other posts from Stack Overflow or by malou17
Published on 2010-02-11T09:03:26Z Indexed on 2010/03/22 8:31 UTC
Read the original article Hit count: 679

Filed under:
|

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();
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms