Windows Develop Bookmark and Share   
 index > Windows Forms General > datagridview
 

datagridview

Hi all,

how to make the datagridview more nicer to the user? I'm developing window base application and the datagridview look not nice. How to customize it so that the record pointer (row) can be set to other colour instead of the default gray colour.

The header of the column , i try to set to the colour that i wish, but also fail. Why this happen? I've set the columnHeaderDefaultCellStyle property but it keep on display the gray colour. Is it possible to do it in .NET?

regards,
Terry
  • Moved byJim Zhou - MSFT Monday, June 08, 2009 12:48 PMDataGridView issue (From:Windows Presentation Foundation (WPF))
  •  
Terry_1314  Friday, June 05, 2009 9:34 AM

Hi Terry_1314,

You can use GDI to make the Winform DataGridView look better. But if you want advanced beautify, you'd better choose WPF.

Please look at this beautified DataGridView.

DataGridView

Here is the code of that DataGridView. The sample override the OnCellPainting method to change the color of the header cell.
public class MyDataGridView : DataGridView
{
private Color headerCellBackColor1;
private Color headerCellBackColor2;
private Color headerCellMouseOnColor1;
private Color headerCellMouseOnColor2;

private int currentOverColumn;
private int currentOverRow;
private int totalColumnWidth;
private int totalRowHeight;

public MyDataGridView()
{
headerCellBackColor1 = Color.FromArgb(198, 226, 255);
headerCellBackColor2 = Color.FromArgb(141, 182, 205);

headerCellMouseOnColor1 = Color.FromArgb(159, 182, 205);
headerCellMouseOnColor2 = Color.FromArgb(185, 211, 238);

this.ColumnHeadersHeight = 50;
}

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
{
if (e.ColumnIndex == this.Columns.Count - 1 || e.RowIndex == this.Rows.Count - 1)
{
totalColumnWidth = e.CellBounds.X + e.CellBounds.Width;
totalRowHeight = e.CellBounds.Y + e.CellBounds.Height;
}

if (e.RowIndex == currentOverRow && e.ColumnIndex == currentOverColumn)
{
FillRectangleColor(e.Graphics, headerCellMouseOnColor1, headerCellMouseOnColor2, e.CellBounds);
e.PaintContent(e.CellBounds);
e.Handled = true;
}
else
{
FillRectangleColor(e.Graphics, headerCellBackColor1, headerCellBackColor2, e.CellBounds);
e.PaintContent(e.CellBounds);
e.Handled = true;
}
}
base.OnCellPainting(e);
}

protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
{
currentOverColumn = e.ColumnIndex;
currentOverRow = e.RowIndex;
base.OnCellMouseMove(e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
if (e.X <= 0 || e.Y <= 0 || e.X >= totalColumnWidth || e.Y >= totalRowHeight)
{
currentOverColumn = 0;
currentOverRow = 0;
}
base.OnMouseMove(e);
}

private void FillRectangleColor(Graphics graphics, Color color1, Color color2, Rectangle rec)
{
LinearGradientBrush brush = new LinearGradientBrush(rec, color1, color2, LinearGradientMode.ForwardDiagonal);
graphics.FillRectangle(brush, rec);
}
}

You can download the whole sample from my skydrive.
http://cid-380a93ce0876cf8b.skydrive.live.com/self.aspx/Microsoft%20MSDN%20Work/Solution%20Code/DataGridView%7C_GDI.rar

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byTerry_1314 Tuesday, June 09, 2009 9:55 AM
  •  
Kira Qian  Tuesday, June 09, 2009 2:39 AM
DataGridView is Windows forms, not WPF, right?

Mark

Mark Salsbery Microsoft MVP - Visual C++
Mark Salsbery [MVP]  Friday, June 05, 2009 6:29 PM

Hi Terry_1314,

You can use GDI to make the Winform DataGridView look better. But if you want advanced beautify, you'd better choose WPF.

Please look at this beautified DataGridView.

DataGridView

Here is the code of that DataGridView. The sample override the OnCellPainting method to change the color of the header cell.
public class MyDataGridView : DataGridView
{
private Color headerCellBackColor1;
private Color headerCellBackColor2;
private Color headerCellMouseOnColor1;
private Color headerCellMouseOnColor2;

private int currentOverColumn;
private int currentOverRow;
private int totalColumnWidth;
private int totalRowHeight;

public MyDataGridView()
{
headerCellBackColor1 = Color.FromArgb(198, 226, 255);
headerCellBackColor2 = Color.FromArgb(141, 182, 205);

headerCellMouseOnColor1 = Color.FromArgb(159, 182, 205);
headerCellMouseOnColor2 = Color.FromArgb(185, 211, 238);

this.ColumnHeadersHeight = 50;
}

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
{
if (e.ColumnIndex == this.Columns.Count - 1 || e.RowIndex == this.Rows.Count - 1)
{
totalColumnWidth = e.CellBounds.X + e.CellBounds.Width;
totalRowHeight = e.CellBounds.Y + e.CellBounds.Height;
}

if (e.RowIndex == currentOverRow && e.ColumnIndex == currentOverColumn)
{
FillRectangleColor(e.Graphics, headerCellMouseOnColor1, headerCellMouseOnColor2, e.CellBounds);
e.PaintContent(e.CellBounds);
e.Handled = true;
}
else
{
FillRectangleColor(e.Graphics, headerCellBackColor1, headerCellBackColor2, e.CellBounds);
e.PaintContent(e.CellBounds);
e.Handled = true;
}
}
base.OnCellPainting(e);
}

protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
{
currentOverColumn = e.ColumnIndex;
currentOverRow = e.RowIndex;
base.OnCellMouseMove(e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
if (e.X <= 0 || e.Y <= 0 || e.X >= totalColumnWidth || e.Y >= totalRowHeight)
{
currentOverColumn = 0;
currentOverRow = 0;
}
base.OnMouseMove(e);
}

private void FillRectangleColor(Graphics graphics, Color color1, Color color2, Rectangle rec)
{
LinearGradientBrush brush = new LinearGradientBrush(rec, color1, color2, LinearGradientMode.ForwardDiagonal);
graphics.FillRectangle(brush, rec);
}
}

You can download the whole sample from my skydrive.
http://cid-380a93ce0876cf8b.skydrive.live.com/self.aspx/Microsoft%20MSDN%20Work/Solution%20Code/DataGridView%7C_GDI.rar

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byTerry_1314 Tuesday, June 09, 2009 9:55 AM
  •  
Kira Qian  Tuesday, June 09, 2009 2:39 AM
this is fantastic. Thanks a lot man. :) great help.


P/S: sorry as i think i've posted in wrong thread.
Terry_1314  Tuesday, June 09, 2009 9:55 AM

You can use google to search for other answers

Custom Search

More Threads

• PropertyGrid - ListBox/ComboBox with auto filter.
• C# Printing Problem
• How to make a C# WinForm application as OLE Server?
• How to get mouse button state at any time?
• Custom datagrid control
• HTTPWebRequest in a Windows Application
• Question about rtf format
• Winform .Net 2.0 error
• How do you trigger the ProgressChanged event of the BackgroundWorker???
• log4net and program files