Filtering DataGridView without changing datasource

前端 未结 7 1829
渐次进展
渐次进展 2020-11-22 06:55

I\'m developing user control in C# Visual Studio 2010 - a kind of \"quick find\" textbox for filtering datagridview. It should work for 3 types of datagridview datasources:

相关标签:
7条回答
  • 2020-11-22 07:49

    A simpler way is to transverse the data, and hide the lines with the Visible property.

    // Prevent exception when hiding rows out of view
    CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView3.DataSource];
    currencyManager.SuspendBinding();
    
    // Show all lines
    for (int u = 0; u < dataGridView3.RowCount; u++)
    {
        dataGridView3.Rows[u].Visible = true;
        x++;
    }
    
    // Hide the ones that you want with the filter you want.
    for (int u = 0; u < dataGridView3.RowCount; u++)
    {
        if (dataGridView3.Rows[u].Cells[4].Value == "The filter string")
        {
            dataGridView3.Rows[u].Visible = true;
        }
        else
        {
            dataGridView3.Rows[u].Visible = false;
        }
    }
    
    // Resume data grid view binding
    currencyManager.ResumeBinding();
    

    Just an idea... it works for me.

    0 讨论(0)
提交回复
热议问题