Aligning Columns In a DataGrid

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hey folks,

Got a Windows Form DataGrid that I bind to a dataset. Using the Table
Styles and the Grid Column Styles, I tell it to right align the columns
which have a numeric value (like Excel does).

Problem is, it also right aligns the Column Header/Description in the
process. The client is asking me to prevent this from happening. Is it
possible to right align the data, but left align it's column headers? If
so, how?

Thanks!
 
You need to a custom DataGridTextBoxColumn to always draw the the cell
contents right aligned.

public class RightAlignedDataGridTextBoxColumn : DataGridTextBoxColumn
{
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)
{
base.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, true);
}
}

It does not work if you set the alignment to centered.

Regards,
Phil.
 
Back
Top