我正在尝试使我的datagridview可搜索,但我似乎无法掌握它。我已经尝试使用谷歌搜索解决方案了,而我发现了以下内容:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value.ToString().ToLower().Contains(searchTextBox.Text.ToLower()) && row.Cells[0] != null)
{
row.Visible = true;
}
else
{
row.Visible = false;
}
}
上面的代码链接到表单上的按钮,而不是很多。当我在文本框中输入一个字符串,然后单击该按钮时,我得到以下异常:
与货币经理的头寸相关联的行不能隐藏。
我不知道如何处理这个异常,我尝试了各种变化,但没有任何帮助。
只是为了给你一个想法,这是我的表格代码(我只有1个表格):
namespace IntegratorReader
{
public partial class start : Form
{
DataTable dt = new DataTable();
public start()
{
InitializeComponent();
}
private void bOpenFileDialog_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((theDialog.OpenFile()) != null)
{
string fullPath = theDialog.FileName;
string fileName = theDialog.SafeFileName;
MessageBox.Show(fullPath);
fillGridView(fullPath);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void fillGridView (string path)
{
var regex = new Regex("\\\"(.*?)\\\"");
System.IO.StreamReader file = new System.IO.StreamReader(path);
string[] columnnames = file.ReadLine().Split(',');
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
newline = regex.Replace(newline, m => m.Value.Replace(',', ' '));
DataRow dr = dt.NewRow();
string[] values = newline.Split(',');
for (int i = 0; i < values.Length; i++)
{
string v = values[i];
v = v.Replace("\"", "");
dr[i] = v;
}
dt.Rows.Add(dr);
}
file.Close();
dataGridView1.DataSource = dt;
}
private void searchButton_Click(object sender, EventArgs e)
{
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value.ToString().ToLower().Contains(searchTextBox.Text.ToLower()) && row.Cells[0] != null)
{
row.Visible = true;
}
else
{
row.Visible = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
简短的例子:
我打开一个包含很多行的.txt文件,而我从第一行开始,并将列基于该行。然后我继续阅读这些行并将它们放在基于“,”分离器的列中。我意识到这可能是用某种CSV读取器完成的,但我需要这个才能工作。这是一个与工作相关的事情,所以我不能给你确切的.txt文件,但它通常看起来像这样,只有更多行和更多列:
“的customerID”, “客户名称”
“1”, “鲍勃”
“2”, “丹尼斯”
“3”, “理查德”
“4”, “费尔南多”
你得到了一般的想法......
现在,我的问题是,我是否完全不知道如何搜索数据网格视图,或者它只是一个我正在监督的小问题? 有没有更好的方法来搜索数据网格视图,如果是,怎么做?
作为参考,这是表单的外观:
答案 0 :(得分:1)
问题与this非常重复:
您的代码存在两个问题,1-它不执行空行检查(最后一行),并且在隐藏行时不会挂起currencyManager。
要修复此问题,请使用以下代码替换searchButton_Click代码:
private void searchButton_Click(object sender, EventArgs e)
{
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
currencyManager1.SuspendBinding();
if (row.IsNewRow)
continue;
if (row.Cells[0].Value.ToString().ToLower().Contains(searchTextBox.Text.ToLower()) )
{
row.Visible = true;
}
else
{
row.Visible = false;
}
currencyManager1.ResumeBinding();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
**回答更新**
另一种选择是在删除OR隐藏行之前将当前单元格设置为null,因为错误告诉“
行与货币管理器相关联
”。
if(dataGridView1.CurrentCell!=null)
dataGridView1.CurrentCell = null;
row.Visible = true;//row.Visible = false