DataGridView Cell Formatting
Hello,
I developed a small win forms application in VS 2005 Beta 1 (which runs perfectly) and recently converted it to Beta 2 and I am seeing strange behavior with formatting of a dataGridView.
I'm trying to find out if this is a bug or not.
After programmatically adding a datatable to the datagridview I am calling a function to format different columns to be currency or have a red forecolor, etc.
I also have a "refresh" button above the grid that calls the same format function.
Here's the problem, when the win form opens up for the first time it has no formatting? But after I click the "refresh" button (which calls the same function as the constructor of the form), the datagridview is suddenly formatted as expected.
Example of the code:
//Form constructor
publicMain()
{
InitializeComponent();
BindDataGrid();//Binds a datatable to the datagridview
FormatDataGrid();//Formats the grid usingDataGridViewCellStyle
//HERE'S THE PROBLEM, RIGHT HERE NO FORMATTING HAS OCCURED?
}//Event handler for button
private void btnRefresh_Click(object sender, EventArgs
e)
{
BindDataGrid();
FormatDataGrid(); //BUT HERE THE FORMATTING DID WORK?
}I'd appriecate if someone could tell me if this is bug in Beta 2 or if I'm simply overlooking something.Thanks,
Paulvo.
[2050 byte] By [
Paulvo] at [2008-2-16]
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.
As a follow up we are reporting this issue to MS. The work around we found was to call the formatting in the pre-paint event handler for rows (
RowPrePaint). Since we set the style of the control to double-buffer we do not get any flickering. The only impact might be performance as this is called often. Nevertheless, it works for now. Our formatting is based on rows so this is a good place for us. If yours is grid specific, cell specific or column specific, play around with the right event for you. Note, this is just a workaround.