Datagridview sort resets my formatting

B

Ben456

Hello,

I'm trying to figure out how to keep my cell formatting the same even
after I've clicked a column sort header.

Basically I've programmatically changed forecolors and backcolors of
several different cells in my datagridview. When I the click the sort
header it resorts the data but all my color formatting is lost. Is
there a way to not lose all this formatting when I sort?

Thanks in advance for any help you can provide,

Ben
 
J

jp2msft

Hey Ben,

In the Design View, Select your DataGridView Control, and go to the
Properties section.

Specify your Alternating Row formatting in the
AlternatingRowsDefaultCellStyle, located under the Appearance category.

Your cells should not be changing colors like that.

Hope that helps!
~Joe
 
R

Rich P

Hi Ben,

My explanation is based on the understanding that the data being
displayed in your datagridview is contained in a dataTable - the
dataTable is the datasource for the datagridview.

When some actions in your code causes a cell to change color - you have
to record/store the row number and column number (or just what field
name) and what color it was changed to - easiest to store in another
dataTable. When you change the sort order of a datagridview the row
numbers of the datagridview change. So you cannot use the datagridview
row numbers (e.RowIndex) and you also cannot use a currencyManager
object because it behave similar to e.RowIndex. You have to identify
the row using the row's Identity field (if your table does not have an
Identity column - you will need one) The way to do this is as follows:

Dim drF() As DataRow = dsMain.Tables("yourTable").Select("rowID = " &
datagridview1.rows(e.rowIndex).Cells("theCellwithRowID").Value)

Then on the datagridview_ColumnHeaderMouseClick event - you have to loop
through all the rows where cell colors have changed (loop through your
other datatable which contains this information) and find each row using
the DataRow array (as above) and programmatically reset each background
color. You have to loop through all the rows in the datagridview and
when a rowID matches - you reset the color for the respective cell.

Once you get the code going - it works pretty smoothly. It isn't that
bad. Just remember that in .Net you are dealing with kind of a lower
level system - meaning you will have to write your own code to make
stuff happen - not as low level as MFC, but not too much higher. So
there aren't as many built in things. You have to build them in
yourself. Afterall, .Net is a programming platform.



Rich
 
B

Ben456

Hi Ben,

My explanation is based on the understanding that the data being
displayed in your datagridview is contained in a dataTable -  the
dataTable is the datasource for the datagridview.  

When some actions in your code causes a cell to change color - you have
to record/store the row number and column number (or just what field
name) and what color it was changed to - easiest to store in another
dataTable.  When you change the sort order of a datagridview the row
numbers of the datagridview change. So you cannot use the datagridview
row numbers (e.RowIndex) and you also cannot use a currencyManager
object because it behave similar to e.RowIndex.  You have to identify
the row using the row's Identity field (if your table does not have an
Identity column - you will need one)  The way to do this is as follows:

Dim drF() As DataRow = dsMain.Tables("yourTable").Select("rowID = " &
datagridview1.rows(e.rowIndex).Cells("theCellwithRowID").Value)

Then on the datagridview_ColumnHeaderMouseClick event - you have to loop
through all the rows where cell colors have changed (loop through your
other datatable which contains this information) and find each row using
the DataRow array (as above) and programmatically reset each background
color.  You have to loop through all the rows in the datagridview and
when a rowID matches - you reset the color for the respective cell.  

Once you get the code going - it works pretty smoothly.  It isn't that
bad.  Just remember that in .Net you are dealing with kind of a lower
level system - meaning you will have to write your own code to make
stuff happen - not as low level as MFC, but not too much higher.  So
there aren't as many built in things.  You have to build them in
yourself.  Afterall, .Net is a programming platform.

Rich

*** Sent via Developersdexhttp://www.developersdex.com***



Thanks Rich,

That's exactly the situation I'm in. I guess I was expecting the
datagridview to have some 'color memory.'
So, I guess I'll have to programmatically reapply the colors to the
datagridview every time I sort ... I was hoping to avoid that ;)

Thanks again for your quick response and very pointed help,

Ben
 
J

Jack Jackson

Thanks Rich,

That's exactly the situation I'm in. I guess I was expecting the
datagridview to have some 'color memory.'
So, I guess I'll have to programmatically reapply the colors to the
datagridview every time I sort ... I was hoping to avoid that ;)

Thanks again for your quick response and very pointed help,

Ben

When the grid is sorted, all of the old DataGridViewRow objects are
destroyed and new ones are created, so any changed you have made to
the row or cell objects get lost.

You might want to check out DataGridViewRow.DataBoundItem. That
property contains a reference to the object from the data source that
is displayed in the row. If you remember that along with the
formatting information, you can loop through the rows after the sort
looking for matching DataBoundItem objects to determine which row is
which.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top