c#中使用sql server的Datagrid过滤器 [英] Datagrid filter in c# using sql server

查看:30
本文介绍了c#中使用sql server的Datagrid过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤datagrid 中的数据,例如如果您选择student 数字中的组合框,然后在文本字段中输入1001.1001 中的所有记录都会出现在 datagrid 中.我正在使用 sql server

How to filter data in datagrid for example if you select the combo box in student number then input 1001 in the text field. All records in 1001 will appear in datagrid. I am 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();
    }
}

推荐答案

你没有说明你注释掉的代码有什么问题.您也没有说明 Studno 列是什么类型.

You don't say what is wrong with the code you commented out. You also don't say what type the Studno column is.

您是否尝试过类似的操作:

Have you tried something like:

ds1.Tables[0].DefaultView.RowFilter = "Studno = '" + txtvalue.text + "'";

这篇关于c#中使用sql server的Datagrid过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆