Answer the question
In order to leave comments, you need to log in
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 = true
Rows.Visible = false
TreeView1_AfterSelect
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
Classes.TreeViewFilter treeViewFilter = new Classes.TreeViewFilter();
treeViewFilter.Filter(this);
}
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;
DataGridView1_CellValueChanged
new 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();
}
}
DataGridView
is disabled! The result of a simple After_Select
. Answer the question
In order to leave comments, you need to log in
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:
CurrencyManager currencyManager = (CurrencyManager)BindingContext [dataGridView1.DataSource];
currencyManager.SuspendBinding();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question