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 に答える