Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Data Grid View Control Row Headers and Column Headers in Visual Studio 2005 C# DotNet
 

Data Grid View Control Row Headers and Column Headers in Visual Studio 2005 C# DotNet

Hi

I am getting the problem in changing the Default Back Color of the Column header and the Row Header of the DataGridView Control in Visual Studio 2005 C# Dot Net.

Can you please give the solution or the way around.

Ajay Kumar

Software Engineer

Metacube Softwares Pvt.Ltd.

Sitapura ,Jaipur.

Ajay_1981  Friday, September 08, 2006 8:54 AM

You will have to draw the row and column header yourself in the paint event to change the back color. The row # is -1 for the row header and the column # is -1 for the column header.


private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex >= 0)
{
StringFormat sf = new StringFormat();
sf.Alignment =
StringAlignment.Center;
sf.LineAlignment =
StringAlignment.Center;
e.Graphics.FillRectangle(
Brushes.Green, e.CellBounds );
e.Graphics.DrawRectangle(
Pens.Gray, e.CellBounds);
SolidBrush br = new SolidBrush(e.CellStyle.ForeColor);
if (dataGridView1.Rows[e.RowIndex].Selected)
{
br =
new SolidBrush(Color.White);
}
e.Graphics.DrawString(dt1.Columns[e.RowIndex].ColumnName.ToString(),
this.Font, br, e.CellBounds, sf );
e.Handled =
true;
br.Dispose();
}
}


Ken Tucker  Friday, September 08, 2006 3:57 PM

You will have to draw the row and column header yourself in the paint event to change the back color. The row # is -1 for the row header and the column # is -1 for the column header.


private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex >= 0)
{
StringFormat sf = new StringFormat();
sf.Alignment =
StringAlignment.Center;
sf.LineAlignment =
StringAlignment.Center;
e.Graphics.FillRectangle(
Brushes.Green, e.CellBounds );
e.Graphics.DrawRectangle(
Pens.Gray, e.CellBounds);
SolidBrush br = new SolidBrush(e.CellStyle.ForeColor);
if (dataGridView1.Rows[e.RowIndex].Selected)
{
br =
new SolidBrush(Color.White);
}
e.Graphics.DrawString(dt1.Columns[e.RowIndex].ColumnName.ToString(),
this.Font, br, e.CellBounds, sf );
e.Handled =
true;
br.Dispose();
}
}


Ken Tucker  Friday, September 08, 2006 3:57 PM

You can use google to search for other answers

Custom Search

More Threads

• How to Add Short Key (Ctrl+Alt+C) to a windows form control
• Strange Unhandled sqlException
• .net 2.0 - datagridview clear a cell
• Master-Detail Form Help needed!!!
• ColumnHeader mouse click crashes application...
• DataGridView virtual mode invoking CellValueNeeded for entire RowCount
• binding datagrid column
• Databinding child relations
• DataGridView - what we are all saying here
• Expand ComboBox ItemList on MouseOver