c#数据网格视图搜索 [英] c# data grid view search
本文介绍了c#数据网格视图搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Windows应用程序中,我有一个数据网格视图。有一些下拉菜单和按钮。根据用户的选择数据网格视图将充满数据..当用户在文本框中键入一些单词并且在该数据网格视图之后应该显示与其相关的记录时,我想要该数据的搜索选项。
请帮帮我
In my windows application i have a data grid view. There are some dropdowns and buttons. According to user's selection data grid view will be full with data.. ow i want search option for that data when user type some words in text box and after that datagrid view should display records related to it.
please help me
推荐答案
为什么要使用row.Cells [row.Index]。您需要指定要搜索的列的索引(问题#2)。例如,您需要将row.Cells [row.Index]更改为row.Cells [2],其中2是列的索引:
private void btnSearch_Click(对象发送者,EventArgs e)
{
string searchValue = textBox1.Text;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach(dgvProjects.Rows中的DataGridViewRow行)
{
if(row.Cells [2] .Value.ToString()。Equals(searchValue))
{
row.Selected = true;
休息;
}
}
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Why you are using row.Cells[row.Index]. You need to specify index of column you want to search (Problem #2). For example, you need to change row.Cells[row.Index] to row.Cells[2] where 2 is index of your column:
private void btnSearch_Click(object sender, EventArgs e)
{
string searchValue = textBox1.Text;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dgvProjects.Rows)
{
if (row.Cells[2].Value.ToString().Equals(searchValue))
{
row.Selected = true;
break;
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
这篇关于c#数据网格视图搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文