Search This Blog

Sunday, March 30, 2014

Formating in DataGridView

To Set Alternating Row Styles for the Windows Forms DataGridView Control

With the DataGridView control, you can specify complete style information for alternating rows. This enables you use style characteristics like foreground color and font, in addition to background color, to differentiate alternating rows.

this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
    Color.Beige;
To specify the font used by DataGridView cells
  • Set the Font property of a DataGridViewCellStyle

this.dataGridView1.DefaultCellStyle.Font = new Font("Verdana", 15);

To specify the foreground and background colors of DataGridView cells

Set the ForeColor and BackColor properties of a DataGridViewCellStyle.
this.dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;

To specify the foreground and background colors of selected DataGridView cells

  • Set the SelectionForeColor and SelectionBackColor properties of a DataGridViewCellStyle.
this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;

Use the Row Template to Customize Rows in the Windows Forms DataGridView Control
The DataGridView control uses the row template as a basis for all rows that it adds to the control either through data binding or when you call theDataGridViewRowCollection.Add method without specifying an existing row to use.
The row template gives you greater control over the appearance and behavior of rows than the RowsDefaultCellStyle property provides. With the row template, you can set anyDataGridViewRow properties, including DefaultCellStyle.
There are some situations where you must use the row template to achieve a particular effect. For example, row height information cannot be stored in a DataGridViewCellStyle, so you must use a row template to change the default height used by all rows. The row template is also useful when you create your own classes derived from DataGridViewRow and you want your custom type used when new rows are added to the control.
  • Set properties on the object retrieved from the DataGridView.RowTemplate property.

DataGridViewRow row = this.dataGridView1.RowTemplate;
row.DefaultCellStyle.BackColor = Color.Bisque;
row.Height = 35;
row.MinimumHeight = 20;


No comments:

Post a Comment