如何根据适用于多列的搜索值显示行 [英] How to display rows based on search value which works for multiple columns

查看:59
本文介绍了如何根据适用于多列的搜索值显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个问题,我必须根据通过文本框显式给出的键值在datagrid视图中搜索行。此键值应为datagridview中的任何列。
但不幸的是,它仅适用于一列(在下面的代码列中为b)

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;

//其中b是表me中的一列。我需要此函数才能用于多列
dv.RowFilter = b =’ + ss +’;
ds.Tables.Clear();
ds.Tables.Add(dv.ToTable());
ds.AcceptChanges();
da.Update(dt);

//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视图

Datagrid View before selecting

推荐答案

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";

编辑:

请考虑以下示例,

    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 + "%'";
    }

这篇关于如何根据适用于多列的搜索值显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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