Discussion:
DataGridView clearing row error text
(too old to reply)
Chuck P
2006-09-21 18:52:43 UTC
Permalink
I have a DataGridView where the user can make changes.
When he goes to quit he can either save or discard the changes.

I don't know if this is the best way to do it but I wrote

(this.BindingSource.DataSource as DataSet).RejectChanges();

object tempDataSource =dataGridView1.DataSource;
dataGridView1.DataSource = null;
dataGridView1.DataSource = tempDataSource;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
dataGridView1.Rows[i].ErrorText = "";

dataGridView1.Refresh();


The original values are back in the grid but the error text/icon
remains.


p.s. I know that if their is an error in the dataset that the error
automatically gets put in the dataGridView1.Rows[i].ErrorText and
that trying to clear it doesn't work. You have to clear it from the
datarowview. So maybe RejectChanges is not clearing the error text in
the dataset?
Jeffrey Tan[MSFT]
2006-09-22 07:42:47 UTC
Permalink
Hi Chuck ,

Yes, based on the RejectChanges method logic, it will only roll back the
data changes to the data, it will not reset the ErrorText on row or cells.

To clear the error icon of the DataGridView, we may clear all the ErrorText
in the datasource, then the error icon will disappear. This is because the
.Net winform databinding will update your changes in datasource into the UI
DataGridViewRow.ErrorText property. My testing code snippet is listed below:

DataTable dt = null;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("column1", typeof(int));
dt.Columns.Add("column2", typeof(string));
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
this.dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
this.dt.Rows[1].RowError = "abc";
}

private void button2_Click(object sender, EventArgs e)
{
dt.RejectChanges();
for (int i = 0; i < dt.Rows.Count; i++)
{
this.dt.Rows[i].RowError = "";
}

dataGridView1.Refresh();
}

This code snippet works well on my side. Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Loading...