datagrid视图从列表中选择并从文本框中搜索

时间:2012-07-02 12:05:29

标签: c# winforms datagrid

在我的datagridview中,我有4列和一个要搜索的文本框。当用户选择列名并在文本框中键入内容时,我在组合框列表中加载了4个列名, 我想指出datagird中的行并将其显示给显示模型。

我有DisplaySelectedProjection函数,当用户选择datagirdview中的任何一行时,所选行的值显示在相应行的文本框中。

private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }   

 private void DataGridDepthProjection_SelectionChanged(object sender, EventArgs e)
    {
        DisplaySelectedProjection();
    }

    private void DisplaySelectedProjection()
    {
        if (DataGridDepthProjection.CurrentRow == null)
            return;

        var index = DataGridDepthProjection.CurrentRow.Index;
        if (index < 0 || index >= bindingList.Count)
            return;

        var item = bindingList[index];
        var depthProjection = depthProjections[item];

        Display(depthProjection);
    }

1 个答案:

答案 0 :(得分:0)

您需要遍历DataGridView行和单元格以匹配条件。为此,您需要使用:

foreach (DataGridViewRow row in this.dataGridView1.Rows)
{                            
    foreach (DataGridViewCell cell in row.Cells)
    {
         /* Your code here */
    }
}

在DataGridView的RowChanging事件中,您需要更改单元格的颜色。