datagridview行搜索。 [英] datagridview row search.

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

问题描述

我的datagridview存在于一个窗体中我可以使用另一种形式搜索datagrideview行,如名称,电子邮件等等

my datagridview is present in one windows form can i search datagrideview rows using another form ,like name wise,email wise etc

推荐答案

您可以使用委托来执行此操作然后使用以下方法:

方式1.

如果你的DataGridView正在使用DataTable或DataView那么

创建一个BindingSource并生成BindingSource。 DataSource将你的DataGridView.DataSource设置为BindingSource。

现在你可以使用BindingSource.Filter按照查询进行过滤。



方式2。

首先在DAtaView对象中收集DataTable.DefaultView,然后使用DataView.RowFilter。根据查询应用过滤器。
You can do that using delegate and then use following ways:
Way 1.
if your DataGridView is using DataTable or a DataView then
Create a BindingSource and make BindingSource.DataSource set your DataGridView.DataSource to the BindingSource.
now you can use BindingSource.Filter to filter as per query.

Way 2.
first collect DataTable.DefaultView in DAtaView object then Use DataView.RowFilter. apply filter as per query.


试试这个:



搜索文本框onchange事件:< br $>


Try this :

on search textbox onchange event :

DataTable tbl = (DataTable)grd.DataSource;
DataView dv = tbl.DefaultView;

dv.RowFilter = "Name Like '" + txtSearch.Text + "%'";


这是我每行搜索DGV的方式:

This is how I search my DGV per row:
int rowIndex = -1;
bool found = false; 

dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
    int searchValue = int.Parse(tbSearchValue.Text);

    foreach (DataGridViewRow row in dgvProjects.Rows)
    {
        int compareValue = int.Parse(row.Cells[2].Value.ToString());

        if (compareValue.Equals(searchValue))
        {
            found = true; 
            rowIndex = row.Index;
            dgvProjects.Rows[row.Index].Selected = true; 
            dgvProjects.FirstDisplayedScrollingRowIndex = rowIndex; 
            break;
        }
    }

    if (!found)
        MessageBox.Show("Project number not found.\n\nBe sure you're searching for the right project number.", "Project not found",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (FormatException)
{
    MessageBox.Show("Your input is not a number.", "Input Error",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
}



在这一行:


In this line:

int compareValue = int.Parse(row.Cells[2].Value.ToString());

row.Cells [2] .Value 代表我的DGV的第二列。我希望我的程序将文本框中的文本与第二列中的文本进行匹配。

row.Cells[2].Value stands for the second column of my DGV. I wanted my program to match the text in the textbox with the text in the second column.


这篇关于datagridview行搜索。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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