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.

DataGridView Cell Formatting
KevinEE
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
Christian_G
If you move the formatting into the Form Load event (or override the form etc.), you will not see this issue.
Kevin Moore - MSFT
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"< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Jon Risbey
casey7
Abubakr
Can you post the code, or a simplified version that reproduces the problem
Faisal Abid
public partial class < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main : Form< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
{
publicMain ()
{
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.