Here's an example of my code:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
//Get the data...
BindDataGrid();
//Call the formatting function...
FormatDataGrid(); //This call does not format the grid.
}
private void BindDataGrid()
{
OleDbDataAdapter da = DB.GetAllEntries(); //DB = static database class
DataSet ds = new DataSet();
da.Fill(ds);
dgrdEntries.DataSource = ds.Tables[0].DefaultView;
lblRowCount.Text = "Rows: " + ds.Tables[0].Rows.Count.ToString();
ListUserTotals();
}
private void FormatDataGrid()
{
//Change font
dgrdEntries.RowsDefaultCellStyle.Font = new Font("arial", 7);
//Change [Amount] and [Deduction] columns to be currency format
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
cellStyle.Format = "C";
dgrdEntries.Columns["Amount"].DefaultCellStyle = cellStyle;
dgrdEntries.Columns["Deduction"].DefaultCellStyle = cellStyle;
//Change [Total] column to have darkred font color and currency
cellStyle = new DataGridViewCellStyle();
cellStyle.ForeColor = Color.DarkRed;
cellStyle.Format = "C";
dgrdEntries.Columns["Total"].DefaultCellStyle = cellStyle;
dgrdEntries.Columns["Owed Amount"].DefaultCellStyle = cellStyle;
//Hide two columns.
dgrdEntries.Columns[0].Visible = false;
dgrdEntries.Columns["notes"].Visible = false;
}
//Event handler for "Refresh" button.
private void refreshGridToolStripButton_Click(object sender, EventArgs e)
{
//Get the data...
BindDataGrid();
//Call the formatting function...
FormatDataGrid(); //This formats the grid as expected!
}
}
Hope this helps.