Thursday, October 23, 2008

Small Thing Learned Today

I needed to hide some of the rows of a databound DataGridView at runtime. However, when I added a piece of code to do that,

foreach (DataGridViewRow row in myDataGridView.Rows)
{
if (someCondition)
{
row.Visible = false;
}
}

I was getting an exception sometimes:

'Row associated with the currency manager's position cannot be made invisible.'

I found out that the exception was happening when the row I was trying to set invisible was selected. The solution to this little problem is to change the code the following way:

CurrencyManager currencyManager1 = (CurrencyManager)
BindingContext[myDataGridView.DataSource];
currencyManager1.SuspendBinding();
foreach (DataGridViewRow row in myDataGridView.Rows)
{
if (someCondition)
{
row.Visible = false;
}
}
myBindingSource.ResumeBinding(); // this is myDataGridView's binding source
by . Also posted on my website

No comments: