Jack
Mon Jul 14 17:38:33 CDT 2008
On Mon, 14 Jul 2008 15:08:26 -0700 (PDT), Ben456
<bendespain@gmail.com> wrote:
>On Jul 14, 3:19 pm, Rich P <rpng...@aol.com> wrote:
>> 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 Developersdex
http://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
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.