Datagrid color

Mar 27, 2013 at 10:00 AM
Hy, can someone tell me how can I color a cell in a datagrid? ex: I want to color in Red the cell 2,2. Thank you, and have a nice day.
Mar 29, 2013 at 8:07 AM
Here it is:
            gv[2, 2].View = new SourceGrid.Cells.Views.Cell();
            gv[2, 2].View.ForeColor = Color.Red;
            gv[2, 2].Value = "Red!";
Mar 29, 2013 at 8:27 AM
Thank you for your response. It seems that you are confusing sourcegrid.datagrid with something else.
I am getting the following error: "Cannot apply indexing with [] to an expression of type 'SourceGrid.DataGrid' ". Can you check if your code is for SourceGrid.DataGrid.
Thank you, and have a nice day.
Mar 29, 2013 at 9:09 AM
Edited Mar 29, 2013 at 9:13 AM
Hi.

Ok, sorry.

Here it what I've used for our customer:
    public class RowStateStyleBuilder {
        public static void Apply(DataGridColumn gc) {
            SourceGrid.DataGrid gv = gc.Grid;

            {
                var viewSel = (SourceGrid.Cells.Views.Cell)gc.DataCell.View.Clone();
                viewSel.BackColor = Color.LightCyan;
                viewSel.ForeColor = Color.Black;
                var cond = new SourceGrid.Conditions.ConditionView(viewSel);
                cond.EvaluateFunction = delegate(SourceGrid.DataGridColumn column, int gridRow, object itemRow) {
                    DataRowView drv = (DataRowView)itemRow;
                    return drv != null && column.Index >= gv.FixedColumns && 0 != (drv.Row.RowState & (DataRowState.Modified | DataRowState.Added));
                };
                gc.Conditions.Add(cond);
            }

            {
                var viewDeleted = (SourceGrid.Cells.Views.Cell)gc.DataCell.View.Clone();
                viewDeleted.Font = new Font(gv.Font, FontStyle.Strikeout);
                var cond = new SourceGrid.Conditions.ConditionView(viewDeleted);
                cond.EvaluateFunction = delegate(SourceGrid.DataGridColumn column, int gridRow, object itemRow) {
                    DataRowView drv = (DataRowView)itemRow;
                    return drv != null && column.Index >= gv.FixedColumns && 0 != (drv.Row.RowState & (DataRowState.Deleted));
                };

                gc.Conditions.Add(cond);
            }
        }
    }
It will highlight modified row:
  • Added/Modified: cyan back ground.
  • Deleted: striked out font. (however you need to tweak your datagrid to make the deleted rows visible)
Thanks
Apr 5, 2013 at 10:14 PM
Hy, thank you very much for your response. Well I have tried your code example and it seems that it colors all the row. So I have done the next modification and it works how I needed to do.
            cond.EvaluateFunction = delegate(SourceGrid.DataGridColumn column, int gridRow, object itemRow)
            {
                DataRowView drv = (DataRowView)itemRow;
                return drv != null && column.Index >= gv.FixedColumns && (drv.Row[1].ToString() == "SA");
            }
Using this it will color just the cells that contain the "SA" string. So next I will change "SA" with some values from another datagridview. Thank you kenji_uno for your support, and have a nice day.