Brandon Moore Brandon Moore - 1 year ago 109
C# Question

Reset DataGridView formatting to default

I color invalid data red in a DataGridView. After the user makes changes, I would like to just reset all formatting back to default so I can look for errors and mark them red again without having to worry about which cells have been fixed and need to be set back to normal.

Is there a quick/easy way to do this without resetting the datasource?


Edit: Guess I didn't communicate clearly enough. It's an import app that moves data from a csv file into a sql table. I put the csv data into the dgv and verify that it's not going to cause any errors before importing (i.e. wrong data type or data is too long) and mark those cells red if so.

They hit a 'verify data' button to run this process before importing and when they hit it again I just want to clear the formatting and start over.

Answer Source

I ended up doing the following:

var ds = dgv.DataSource;
dgv.DataSource = null;
dgv.DataSource = ds;

That caused all the formatting to go back to default values like I wanted without having to reload my data. If there's some property or other better way to do this that I'm overlooking though then put it on here so I can mark it as a better answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download