如何在Datagridview中搜索Value? [英] How to search Value within Datagridview ?

查看:102
本文介绍了如何在Datagridview中搜索Value?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在WinForm上保存一些记录的DatagridView

i在我的Datagridview上方有一个文本框用于搜索数据网格中的单行



当某个人在文本框中提供ID时,Datagrid必须通过刷新来回复并仅提供搜索记录



i have a DatagridView Which Hold Some Record on a WinForm
i have one Text box above my Datagridview for Search a single Row in the datagrid

when some one Give Id in the Textbox the Datagrid have to response by refreshing and give only search record

public void searchclient()
        {

            {
                try
                {

                    SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
                    SqlCommand cmd = new SqlCommand("donetask", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    if (conn == null && conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    dt2 = new DataTable("myTable");
                    da.Fill(dt2);
                   
                    dgriddone.DataSource = dt2.DefaultView;
                    
                    DataView dv = new DataView(dt2);
                    dv.RowFilter = "ID="+ "'" + txtsearch.Text + "'";
                    dgriddone.Refresh();
                    
                    dgriddone.DataSource = dt2.DefaultView;
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }

推荐答案

将BindingSource添加到您的系统,并设置Filter属性那个。

见这里:如何排序网格视图数据月份和年份 [ ^ ]
Add a BindingSource to your system, and set the Filter property of that.
See here: How to sort grid view Data Month and year wise[^]


OriginalGriff的解决方案1非常好。



另一种方式是:

1)使用DataTable.Select [ ^ ]方法



2)通过SQL过滤数据库存储过程 [ ^ ]:

Solution 1 by OriginalGriff is very good.

Another way is to:
1) use DataTable.Select[^] method
or
2) to filter database via SQL stored procedure[^]:
CREATE PROCEDURE FilterDataById
    @id INT
AS

BEGIN

    SELECT Field1, Field2, ... FieldN
    FROM TableName
    WHERE ID = @id

END





如需了解更多信息,请参阅:如何:执行返回行的存储过程 [ ^ ]


这篇关于如何在Datagridview中搜索Value?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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