Current filter:
                                You should refresh the page.
                                Support Center
                                0
                                  • How do I set the color or a row based on a value in a column?

                                You must  log in  or  register  to leave comments

                                1 Solution

                                0

                                There are two solutions:
                                1. Define a style condition;
                                2. Handle the RowCellStyle event.
                                This article explains the second solution.

                                You should set the e.Appearance.BackColor parameter within the RowCellStyle event handler. A cell is defined by the e.RowHandle and e.Column parameters; the cell's value is passed to the e.CellValue parameter.

                                Our task is to change the style of the whole row. This means that the e.Column parameter should be ignored. A certain column's value can be obtained via the GetRowCellValue method.

                                [C#]
                                private bool IsShipToUSCanada(GridView view, int row) { try { string val = Convert.ToString(view.GetRowCellValue(row, "ShipCountry")); return (val == "US" || val == "Canada"); } catch { return false; } } private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { if(IsShipToUSCanada(gridView1, e.RowHandle)) { e.Appearance.BackColor = Color.LightGray; } }
                                [VB.NET]
                                Private Function IsShipToUSCanada(ByVal view As GridView, ByVal row As Integer) As Boolean Try Dim val As String = Convert.ToString(view.GetRowCellValue(row, "ShipCountry")) Return (val = "US" OrElse val = "Canada") Catch Return False End Try End Function Private Sub gridView1_RowCellStyle(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs) Handles gridView1.RowCellStyle If IsShipToUSCanada(gridView1, e.RowHandle) Then e.Appearance.BackColor = Color.LightGray End If End Sub

                                If you need to change the style of a single cell (column) instead of a row, you will need to check the e.Column parameter: change e.Appearnace only if e.Column denotes the desired column.

                                See Also:
                                Customizing Appearances of Individual Rows and Cells
                                How to customize the Look-And-Feel of my grid cells

                                You must  log in  or  register  to leave comments

                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners