We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GDBG : Changing color of an entire row based on a cell value

Hi
I am using the GridDataboundGrid control of Syncfusion ver 3.2.1.0.

I am trying to change the text color of an entire row based on the value of a particular cell in that row.

I have placed this code in PrepareViewStyleInfo event handler.

The problem I am facing is that only cells after the cell from which the comparison is made are being affected. The cells before are colored only if they are redrawn, say by re-opening in the window or scrolling out of the cells and back in to them again.

How do I ensure that the entire row's textcolor is changed once I have found which row (rowindex) to change the textcolor of.

Here's my code-
Note: rowIndices is a globally defined ArrayList

private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;

if( e.ColIndex == 8 && e.RowIndex > 0)
{
string s = grid.Model[e.RowIndex,e.ColIndex].Text;
if( s == "Securitized")
{
if(!rowIndices.Contains(e.RowIndex))
{
rowIndices.Add(e.RowIndex);
}
}
}

if(rowIndices.Contains(e.RowIndex))
{
e.Style.TextColor = Color.Red;
}
}

4 Replies

AD Administrator Syncfusion Team June 7, 2007 04:42 PM UTC

After doing your

rowIndices.Add(e.RowIndex);

try adding this call in the same if-block:

grid.RefreshRange(GridRangeInfo.Row(e.RowIndex));



AN Anupama June 8, 2007 10:20 AM UTC

Great! Thanks! It worked.

Now I have another problem.

Here's my working code

private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;
if(e.RowIndex > 0)
{
string msid = grid.Model[e.RowIndex,gridDataBoundGrid1.Binder.NameToColIndex("MortgageID")].Text;

if( e.ColIndex == gridDataBoundGrid1.Binder.NameToColIndex("StatusDescription"))
{
string statusdesc = grid.Model[e.RowIndex,e.ColIndex].Text;
if( statusdesc == "Securitized")
{
if(!rowIndicesRed.ContainsKey(msid))
{
rowIndicesRed.Add(msid,e.RowIndex.ToString());
grid.RefreshRange(GridRangeInfo.Row(e.RowIndex), GridRangeOptions.CalculateNonClientArea);
}
}
if( statusdesc == "Processing")
{
if(!rowIndicesAmber.ContainsKey(msid))
{
rowIndicesAmber.Add(msid,e.RowIndex.ToString());
grid.RefreshRange(GridRangeInfo.Row(e.RowIndex), GridRangeOptions.CalculateNonClientArea);
}
}
}
if(rowIndicesRed.ContainsKey(msid) && e.RowIndex.ToString() == rowIndicesRed[msid])
{
e.Style.TextColor = Color.Red;
}

if(rowIndicesAmber.ContainsKey(msid) && e.RowIndex.ToString() == rowIndicesAmber[msid])
{
e.Style.TextColor = Color.DarkOrange;
}
}
}

Now the problem is that when I apply sorting on one of the columns by using griddataboundgrid1.SortColumn(2), the coloring of rows is all wrong.

The same happens when the user tries to Sort by double-clicking on one of the column headers.

What can I do to correct this?

I have a related question - How can programmatically sort based on 2 columns and also specify the sort order as descending using the GridDataboundGrid control.

Thanks!


AD Administrator Syncfusion Team June 8, 2007 01:05 PM UTC

This is the code that does not make sense when trying to support sorting.

if(rowIndicesRed.ContainsKey(msid) && e.RowIndex.ToString() == rowIndicesRed[msid])


The reason is that the values in rowIndicesRed[msid] reflects the unsorted row index while the e.RowIndex is now the sorted position.

There are several ways around this. The simplest might be just to call rowIndicesRed.Clear followed by grid.Refresh() after a sort. You could do this in a general way by subscribing to the ListChanged event on the datasource and resseting your color lists and redraw things when a Reset comes through.

//assumes your DataSource supports IBindingList
//subscribe to the ListChanged event
GridDataBoundGrid grid = this.gridDataBoundGrid1;
CurrencyManager cm = grid.BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;
IBindingList ibl = cm.List as IBindingList;
ibl.ListChanged += new ListChangedEventHandler(ibl_ListChanged);



//the handler
void ibl_ListChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.Reset)
{
rowIndicesRed.Clear();
rowIndicesAmber.Clear();
this.gridDataBoundGrid1.Refresh();
}
}


To sort a particular column in descending order, you can use:

//assumes your DataSource is a DataTable
CurrencyManager cm = grid.BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;
DataView dv = cm.List as DataView;
dv.Sort = "ColumnName DESC";


AN Anupama June 11, 2007 12:41 PM UTC

Thanks, Clay I will work on those lines but I need a few more clarifications

(1) After clearing the lists and calling griddatboundgrid1.Refresh(), how can I ensure that the PrepareViewStyle event handler is called? Will Refresh() ensure that it is called?

(2) My grid has a lot of columns, some of which cannot be seen until the user pans to the right of the screen. One such column which becomes visible only on panning to the right is the StatusDescription column, based on which the decision to color the row red or amber is taken in PrepareViewStyle event. Because of this, the coloring does not take place until the user actually pans to the right so that StatusDescription comes into view.
Is there a way to ensure that the rows are colored without requiring the column to come into view?

(3) How can I sort based on 2 columns from the code you sent-
//assumes your DataSource is a DataTable
CurrencyManager cm = grid.BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;
DataView dv = cm.List as DataView;
dv.Sort = "ColumnName DESC";

Thanks for your help!

Loader.
Live Chat Icon For mobile
Up arrow icon