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

Alternating Row Colors

I want to alternate row colors and am using the code shown (below) in the FAQ or on the forum, don't recall which. Anyway, this looks terrible! I have a hierachy I am showing and this code does not take that into consideration. So it is possible that items at the same level in the hierarchy are different colors. I want the coloring to be reset for each level in the hierarchy. So the first child at any given depth is always the same color. Other grids just make this happen by setting a top level property, so perhaps this is a suggestion for a future feature. For the time being I would be happy with a work-around. Tx, curt private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e) { if ( e.Style.CellType.CompareTo( "TextBox") == 0 || e.Style.CellType.CompareTo( "CheckBox") == 0) { if ( e.ColIndex > 0 && e.RowIndex > 0) { if ( e.RowIndex % 2 == 0) e.Style.BackColor = Color.LightBlue; else e.Style.BackColor = Color.White; } } }

1 Reply

AD Administrator Syncfusion Team February 24, 2003 06:09 PM

Hi Curtis, try to get the GridBoundRecordState and the position within the nested table. Example: private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e) { GridDataBoundGrid grid = sender as GridDataBoundGrid; if (grid != null && e.ColIndex > 1) { if (grid.IsShowCurrentRow(e.RowIndex) && !grid.CurrentCell.HasCurrentCellAt(e.RowIndex, e.ColIndex)) { e.Style.BackColor = SystemColors.Highlight; e.Style.TextColor = SystemColors.HighlightText; } else { GridBoundRecordState rs = grid.Binder.GetRecordStateAtRowIndex(e.RowIndex); switch (rs.LevelIndex) { case 0: if ( rs.Position %2 == 0) e.Style.BackColor = Color.LightBlue; else e.Style.BackColor = Color.White; break; case 1: if ( rs.Position % 2== 0) e.Style.BackColor = Color.DarkBlue; else e.Style.BackColor = Color.Yellow; break; default: if ( rs.Position %2 == 0) e.Style.BackColor = Color.Blue; else e.Style.BackColor = Color.Fuchsia; break; } } } } We'll add support for automatic alternate row colors together with many other improvements for hierarchical display in the 2.0 version. Stefan

Loader.
Live Chat Icon For mobile
Up arrow icon