Retain DataGrid AutoFormat values

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

I have a DataGrid control on a form and I used the Auto Format feature to format the grid.

The problem is now if I resize a column in my code I loose some of the formats - some of the grid colors returns to default values. Here is the code that I use to resize a column.

DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Language";
DataGridTextBoxColumn grdColStyle1 = new DataGridTextBoxColumn();
grdColStyle1.HeaderText = "Language(s) Spoken";
grdColStyle1.MappingName = "Language";
grdColStyle1.Width = 200;
DataGridColumnStyle [] dgcs = {grdColStyle1};
tableStyle.GridColumnStyles.AddRange(dgcs);
this.dgLanguages.TableStyles.Add(tableStyle);


How can I resize columns and retain current formats?



Peter
 
Hello Peter,

Thanks for posting. According to your description, when you try to resize a
column in the auto-formatted DataGrid, some formats of the column get lost.
If I have misunderstood you, please correct me.

If we add a new DataGridTableStyle to the DataGrid, it will override the
default color settings of the DataGrid, which is set by "Auto Format". As a
result, we need to explicitly set the color of the newly created
DataGridTableStyle, for example:

tableStyle.AlternatingBackColor = dgLanguages.AlternatingBackColor;
tableStyle.BackgroundColor = dgLanguages.BackgroundColor;
...

I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top