2

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";
    }
4

1 回答 1

7

尝试从您的数据表中创建一个 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());
        }
于 2013-06-07T10:28:07.233 回答