C# dataTable.DefaultView.Rowfilter 总是返回第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16981199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dataTable.DefaultView.Rowfilter always returning first row
提问by Aya desarrollos
I've been trying to make a search function for a dataTable. My problem is that the first row of the table is ALWAYS within the filtered rows, even when the boolean column is actually changed to a zero. Here is my search code:
我一直在尝试为数据表创建一个搜索功能。我的问题是表的第一行总是在过滤的行内,即使布尔列实际上更改为零。这是我的搜索代码:
private void buscar()
{
DataTable dataTable;
if (!verTodos)
{
dataTable = DBHelper.Instance.ProductosConStock();
}
else
{
dataTable = DBHelper.Instance.ProductosTodos();
}
dataGridProductos.DataSource = dataTable.DefaultView;
foreach (DataGridViewRow row in dataGridProductos.Rows)
{
if (row.Cells[0].Value.ToString().ToUpper().Contains(txtBusqueda.Text.ToString().ToUpper()) ||
row.Cells[1].Value.ToString().ToUpper().Contains(txtBusqueda.Text.ToString().ToUpper()) ||
row.Cells[2].Value.ToString().ToUpper().Contains(txtBusqueda.Text.ToString().ToUpper()))
{
row.Cells[4].Value = 1;
}
else
{
row.Cells[4].Value = 0;
}
}
dataTable.DefaultView.RowFilter = "mostrar = 1";
}
采纳答案by CathalMF
Try creating a DataView from your datatable and run the filter based on that. Below is a quick example i tried and it works fine.
尝试从您的数据表创建一个 DataView 并基于它运行过滤器。下面是我尝试过的一个快速示例,它工作正常。
DataTable dt = new DataTable();
dt.Columns.Add("bool", typeof(Boolean));
dt.Rows.Add(true);
dt.Rows.Add(false);
dt.Rows.Add(true);
DataView dv = new DataView(dt);
dv.RowFilter = "bool = 1";
foreach (DataRowView drv in dv)
{
Console.WriteLine(drv[0].ToString());
}

