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
public
Main()
{
InitializeComponent();
BindDataGrid();//Binds a datatable to the datagridview
FormatDataGrid();//Formats the grid using
DataGridViewCellStyle
//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]
# 1

Can you post the code, or a simplified version that reproduces the problem?

durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
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.

Paulvo at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
The problem has to do with the fact that the control isn't yet finalized because your work is done in the form constructor. The general guidance is do UI stuff in the Form Load event (or override the forms OnLoad event).

If you move the formatting into the Form Load event (or override the form etc.), you will not see this issue.

durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
I have a similar issue, and am doing everything in Form Load.
However, the column style/property changes work, but row changes do not. The row changes work fine if called from a button later.
Any ideas?
BenGracewood at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
I am having similar problems. Rows Default Style don't work until I call them from a button. They don't work when the form loads up.
RiteshTijoriwala at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
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.
BradEck at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

This was a bug in Beta2 that is fixed in the RTM version. Columns changes done before the grid is fully databound were lost. In the RTM version we preserve column changes (properties applied to columns).

There are a few workarounds:
In your BindDataGrid method, after you set the DataSource of the DataGridView, set the DataGridView's AutoGenerateColumns property to false.

hope this helps,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...