How To Implement Conditional Formatting In the DataGrid

Jan 18, 2011 at 4:58 AM

Hello, 

First of all many thanks for the library authors! Great work and a great component. Its DataGrid allowed me to visualize vert frequently updated data with ease while WindowsForms DataGridView failed.

I have a question about the control.  Is it possible to implement some kind of conditional formatting without iterating over each cell assigning required view? (actually I need to color cells differently upon change).

Thanks in advance.

Coordinator
Jan 18, 2011 at 7:00 AM

Yes, AFAIK that should be possible.

 

Look at exampes, and look at how cell background colours are added. i.e. the alternating colours. 

Feb 27, 2011 at 6:48 PM

I must be missing something...when i try to add a condition to a column for an individual cell (row), each cell in the column takes on the first view I apply.  In this example, I am applying bold, italics and underline styles...so if I bold the first cell and then underline the second, the second cell shows up bold as well. Each view is unique as evidenced by the check at the bottom for each loop (see output).  So why is it that I seemingly can't have more than one conditional view in a column when using a GridView?  I know I am probably doing something dumb, but I just cant see it.

Output: 

B:True  I:False U:False

B:False  I:False U:True

B:False  I:True U:False

 

        public void ApplyCellStyles()
        {
            if (this.DataSource != null)   
            {
                BoundDataView bdv = (BoundDataView)this.DataSource;
                Table t = (Table)bdv.DataTable;

                //Clear out any existing conditions.
                ClearColumnConditions();

                foreach (DBCellStyle style in t.CellStyles)
                {
                    //Get our view from the Table's DBCellStyle.
                    SourceGrid.Cells.Views.Cell cell = (SourceGrid.Cells.Views.Cell)style.ToSourceGridCellView();
                    SourceGrid.Conditions.ConditionView styleCondition = new SourceGrid.Conditions.ConditionView(cell);

                    //Define the function that dermeines whether or not a cell should have this formatting, in our case,
                    //this means it is at the desired location. (Row)
                    styleCondition.EvaluateFunction = delegate(SourceGrid.DataGridColumn column, int gridRow, object itemRow)
                    {
                        //... // not important for this post.
                    };
                    
                    //Add the condition to the cell's column
                    this.Columns[style.ColumnIndex + 1].Conditions.Add(styleCondition);
                }

              
                foreach (SourceGrid.Conditions.ICondition condition in this.Columns[1].Conditions)
                {
                    //Just in case there is a null reference exception or something.
                    try
                    {
                        SourceGrid.Conditions.ConditionView c2 = (SourceGrid.Conditions.ConditionView)condition;
                        Console.WriteLine("B:" + c2.View.Font.Bold.ToString() + "  " + "I:" + c2.View.Font.Italic.ToString() + " " + "U:" + c2.View.Font.Underline.ToString());
                    }

                    catch{}
                }