帮助创建数据网格的过滤器!

时间:2010-12-08 20:46:27

标签: c#

我想创建一个过滤器来过滤数据网格中的文本,就像我在文本框中键入一样。我有两个文本框,一个用于过滤我输入的文本,另一个用于过滤我输入的文本。我希望数据网格在我输入其中一个文本框时更新。以下是我的一些代码:

    private void txtFilterIn_TextChanged(object sender, TextChangedEventArgs e)
    {
        DataView dv = dgPortStatus.DataContext as DataView;
        if (dv != null)
        {
            if (dv.Table.TableName.ToUpper() == "PORTSTATUS")
            {
                FilterDataGrid(txtFilterIn.Text, txtFilterOut.Text, _dicPortStatus[cmbGroups.SelectedItem.ToString()]);
            }
        }

        List<QuarantineView> qvList = dgPortStatus.DataContext as List<QuarantineView>;
        if (qvList != null)
        {
            //Filter on quarantine manager
            FilterQuarantineViewList(qvList);
        }
    }

    private void btnFilterInClear_Click(object sender, RoutedEventArgs e)
    {
        txtFilterIn.Clear();
    }

    private void txtFilterOut_TextChanged(object sender, TextChangedEventArgs e)
    {
        DataView dv = dgPortStatus.DataContext as DataView;
        if (dv != null)
        {
            if (dv.Table.TableName.ToUpper() == "PORTSTATUS")
            {
                FilterDataGrid(txtFilterIn.Text, txtFilterOut.Text, _dicPortStatus[cmbGroups.SelectedItem.ToString()]);
            }
        }

        List<QuarantineView> qvList = dgPortStatus.DataContext as List<QuarantineView>;
        if (qvList != null)
        {
            //Filter on Quarantine Manager
            FilterQuarantineViewList(qvList);
        }
    }

    private void btnFilterOutClear_Click(object sender, RoutedEventArgs e)
    {
        txtFilterOut.Clear();
    }

这个方法是我的过滤器需要去的地方:

private void FilterQuarantineViewList(List<QuarantineView> qvList)
{


}

1 个答案:

答案 0 :(得分:0)

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow r in dataGridView1.Rows)
        {
            r.Visible = false;
            foreach (DataGridViewCell c in r.Cells)
                if (c.Value.ToString().Contains(textBox1.Text))
                {
                    r.Visible = true;
                    break;
                }
        }
    }