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.
Unfortunately, activation email could not send to your email. Please try again.

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

Thread ID:

Created:

Updated:

Platform:

Replies:

62074 Jun 7,2007 06:17 AM Jun 11,2007 08:41 AM Windows Forms 4
loading
Tags: GridControl
Anupama
Asked On June 7, 2007 06:17 AM

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;
}
}

Administrator [Syncfusion]
Replied On June 7, 2007 12:42 PM

After doing your

rowIndices.Add(e.RowIndex);

try adding this call in the same if-block:

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


Anupama
Replied On June 8, 2007 06:20 AM

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!

Administrator [Syncfusion]
Replied On June 8, 2007 09:05 AM

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";

Anupama
Replied On June 11, 2007 08:41 AM

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!

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;