C
C
Collin2019-10-21 07:38:28
C++ / C#
Collin, 2019-10-21 07:38:28

Extra rows appear in DataGridView after applying TreeView filter, how to fix?

There was a problem with displaying rows filtered through TreeView in DataGridVIew.
The essence of the problem: after applying the filter, for example, by condition to make rows that match, and mismatch , other rows appear, and not completely displayed, but only their updated cells, as in the example: It is important to note that my DataGridView is not updated in main thread, i.e. i am constantly updating something in the table by given thread_delay_time. The code that works in the method :Rows.Visible = trueRows.Visible = false
5dad34cd8c488502205105.png
TreeView1_AfterSelect

private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        Classes.TreeViewFilter treeViewFilter = new Classes.TreeViewFilter();
        treeViewFilter.Filter(this);
    }

Snippet from class TreeViewFilter:
class TreeViewFilter
        {
            public void Filter(MainForm mainForm)
            {
                switch (mainForm.TreeView1.SelectedNode.Text)
                {
                    case "Категория":
                        mainForm.dataGridView1.CurrentCell = null;
                        for (int i = 0; i < mainForm.dataGridView1.Rows.Count; i++)
                        {
                            mainForm.dataGridView1.Rows[i].Visible = true;
                        }
                        break;
                    case "МЭД":
                        mainForm.dataGridView1.CurrentCell = null;
                        for (int i = 0; i < mainForm.dataGridView1.Rows.Count; i++)
                        {
                            if (Convert.ToInt32(mainForm.dataGridView1.Rows[i].Cells[13].Value) == 1)
                                mainForm.dataGridView1.Rows[i].Visible = true;
                            else
                                mainForm.dataGridView1.Rows[i].Visible = false;
                        }
                        break;
                    case "Воздух":
                        mainForm.dataGridView1.CurrentCell = null;
                        for (int i = 0; i < mainForm.dataGridView1.Rows.Count; i++)
                        {
                            if (Convert.ToInt32(mainForm.dataGridView1.Rows[i].Cells[13].Value) == 2)
                                mainForm.dataGridView1.Rows[i].Visible = true;
                            }
                            else
                                mainForm.dataGridView1.Rows[i].Visible = false;

... and so on, everything else by analogy.
I need filtering not only by After_Select, but also dynamic, so that when the TreeView node is selected in the DataGridView, only the required set of rows is displayed, I implemented this through a DataGridView1_CellValueChangednew thread in which everything was done by analogy, but with minor changes, tk. an exception popped up:private void ThreeViewMarker()
Invoke((MethodInvoker)delegate
                {
                    switch (TreeView1.SelectedNode.Text)
                    {
                        case "Категория":
                            dataGridView1.CurrentCell = null;
                            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                            {
                        CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                        currencyManager.SuspendBinding();
                        dataGridView1.Rows[i].Visible = true;
                        currencyManager.ResumeBinding();
                            }
                            break;
                        case "МЭД":
                            dataGridView1.CurrentCell = null;
                            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                            {
                                if (Convert.ToInt32(dataGridView1.Rows[i].Cells[13].Value) == 1)
                                {
                            CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                            currencyManager.SuspendBinding();
                            dataGridView1.Rows[i].Visible = true;
                            currencyManager.ResumeBinding();
                                }
                                else
                                {
                            CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                            currencyManager.SuspendBinding();
                            dataGridView1.Rows[i].Visible = false;
                            currencyManager.ResumeBinding();
                                }
                            }
                            break;
                        case "Воздух":
                            dataGridView1.CurrentCell = null;
                            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                            {
                                if (Convert.ToInt32(dataGridView1.Rows[i].Cells[13].Value) == 2)
                                {
                            CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                            currencyManager.SuspendBinding();
                            dataGridView1.Rows[i].Visible = true;
                            currencyManager.ResumeBinding();
                                }
                                else
                                {
                            CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                            currencyManager.SuspendBinding();
                            dataGridView1.Rows[i].Visible = false;
                            currencyManager.ResumeBinding();
                                }
                            }

... and so on, by analogy.
Naturally, this method also does not work properly, extra lines appear.
There is also a strange behavior of lines after applying the filter in general, the problem manifests itself when the window is scaled in a slightly different way and the first line from the set of lines appears:
5dad3584e4923520515099.gif
On .gif, the dynamic update method DataGridViewis disabled! The result of a simple After_Select.
Please tell me how to deal with this and what I did not take into account when filtering like this.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Space Purr, 2019-10-29
@Collin

As far as I understand, the extra line that appears is the highlighted line in the CurrencyManager.
I was able to find two approaches to solve this problem:

  • Disable CurrencyManager at the beginning of the filter tree method
    CurrencyManager currencyManager = (CurrencyManager)BindingContext [dataGridView1.DataSource];
    currencyManager.SuspendBinding();

    Disabling will disable some features, such as CurrentRow, CurrentCell, and possibly something else.
    However, this option does not work when all rows should be hidden.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question