Home > Computers and Internet > How to color a specific colomn of DataGrid

How to color a specific colomn of DataGrid

 

 

1.Create a DataTable with value.

 

#region create SomeDataTable

            private DataTable SomeDataTable()

            {

                  DataTable dt =new DataTable ()      ;

                  dt.Columns.Add (new DataColumn ("Name",Type.GetType("System.String")));

                  dt.Columns.Add (new DataColumn ("Code",Type.GetType("System.String")));

                  dt.Columns.Add (new DataColumn ("Contact",Type.GetType("System.String")));

                  dt.Columns.Add (new DataColumn ("Contact1",Type.GetType("System.String")));

                  dt.Columns.Add (new DataColumn ("Contact2",Type.GetType("System.String")));

                 

                  DataRow dr;

                  dr=dt.NewRow();

                  dr["Name"]="Sanjiv Kumar";dr["Code"]="127";dr["Contact"]="123456";dr["Contact1"]="123456";dr["Contact2"]="123456";

                  dt.Rows.Add(dr);

                  dr=dt.NewRow();

                  dr["Name"]="Sunil";dr["Code"]="123";dr["Contact"]="123456";dr["Contact1"]="123456";dr["Contact2"]="123456";

                  dt.Rows.Add(dr);

                  dr=dt.NewRow();

                  dr["Name"]="Krishna";dr["Code"]="128";dr["Contact"]="123456";dr["Contact1"]="123456";dr["Contact2"]="123456";

                  dt.Rows.Add(dr);

                  dr=dt.NewRow();

                  dr["Name"]="Krishna";dr["Code"]="128";dr["Contact"]="123456";dr["Contact1"]="123456";dr["Contact2"]="123456";

                  dt.Rows.Add(dr);

                  dr=dt.NewRow();

                  dr["Name"]="Krishna";dr["Code"]="128";dr["Contact"]="123456";dr["Contact1"]="123456";dr["Contact2"]="123456";

                  dt.Rows.Add(dr);

                  dr=dt.NewRow();

                  dr["Name"]="Krishna";dr["Code"]="128";dr["Contact"]="123456";dr["Contact1"]="123456";dr["Contact2"]="123456";

                  dt.Rows.Add(dr);

                  dt.DefaultView.AllowNew = false;//turn off append row

                  return dt;

            }

            #endregion

 

2. Create a method for add colomn Style

#region Add ColumnStyles to the Grid

            private void AddCellFormattingColumnStyles(DataGrid grid, FormatCellEventHandler handler)

            {

                  DataGridTableStyle ts = new DataGridTableStyle();

 

                  DataTable dt = (DataTable) grid.DataSource;

 

                  ts.MappingName = dt.TableName;

 

                  for(int j = 0; j < dt.Columns.Count; ++j)

                  {

                        DataGridFormattableTextBoxColumn cs = new DataGridFormattableTextBoxColumn(j);

                        cs.MappingName = dt.Columns[j].ColumnName;

                        cs.HeaderText = dt.Columns[j].ColumnName;

                        cs.SetCellFormat += handler;

                        ts.GridColumnStyles.Add(cs);

                  }

 

                  grid.TableStyles.Clear();

                  grid.TableStyles.Add(ts);

 

            }

 

            #endregion

3. Create a two different Class and delegate

 

 

#region CellFormatting Event

 

      public delegate void FormatCellEventHandler(object sender, DataGridFormatCellEventArgs e);

 

      public class DataGridFormatCellEventArgs : EventArgs

      {

            private int _column;

            private int _row;

            private Font _font;

            private Brush _backBrush;

            private Brush _foreBrush;

            private bool _useBaseClassDrawing;

 

 

            public DataGridFormatCellEventArgs(int row, int col, Font font1, Brush backBrush, Brush foreBrush)

            {

                  _row = row;

                  _column = col;

                  _font = font1;

                  _backBrush = backBrush;

                  _foreBrush = foreBrush;

                  _useBaseClassDrawing = false;

            }

 

            public int Column

            {

                  get{ return _column;}

                  set{ _column = value;}

            }

            public int Row

            {

                  get{ return _row;}

                  set{ _row = value;}

            }

            public Font TextFont

            {

                  get{ return _font;}

                  set{ _font = value;}

            }

 

            public Brush BackBrush

            {

                  get{ return _backBrush;}

                  set{ _backBrush = value;}

            }

            public Brush ForeBrush

            {

                  get{ return _foreBrush;}

                  set{ _foreBrush = value;}

            }

            public bool UseBaseClassDrawing

            {

                  get{ return _useBaseClassDrawing;}

                  set{ _useBaseClassDrawing = value;}

            }

      }

      #endregion

 

     

      #region CustomColumnStyle that handles formatting

      public class DataGridFormattableTextBoxColumn : DataGridTextBoxColumn

      {

            //in your handler, set the EnableValue to true or false, depending upon the row & col

            public event FormatCellEventHandler SetCellFormat;

           

            private int _col;

 

            public DataGridFormattableTextBoxColumn(int col)

            {

                  _col = col;

            }

 

            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)

            {

                  DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs(rowNum, this._col, this.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush);

                  if(SetCellFormat != null)

                  {

                        SetCellFormat(this, e);

                  }

                  if(e.UseBaseClassDrawing)

                        base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);

                  else

                  {

                        g.FillRectangle(e.BackBrush, bounds);

                        g.DrawString(this.GetColumnValueAtRow(source, rowNum).ToString(), e.TextFont, e.ForeBrush, bounds.X, bounds.Y);

                  }

                  if(e.TextFont != this.DataGridTableStyle.DataGrid.Font)

                        e.TextFont.Dispose();

            }

 

            protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)

            {

                  //comment to make cells unable to become editable

                  base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

            }

           

      }

      #endregion

 

4. In any function Call this methods

private void button1_Click(object sender, System.EventArgs e)

{

this.dataGrid1.DataSource = SomeDataTable();

AddCellFormattingColumnStyles(this.dataGrid1, new FormatCellEventHandler(FormatGridCells));

}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: