DataGrid format

P

Peter

I have a datagrid on WinForm and I am trying to format the Grid.
I am using the following code, the code runs fine, but nothing happens after it runs, the datagrid does not change (with data in the grid or with out any data)

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";
DataGrid1.TableStyles.Add(gridStyle);

gridStyle.GridColumnStyles[0].Width = 100;
gridStyle.GridColumnStyles[0].HeaderText="Test Col";
gridStyle.DataGrid.Refresh();


What am I doing wrong?
 
J

Jacob

I believe you need to create some DataGridColumnStyle objects and add them to the DataGridTableStyle BEFORE you add the DataGridTableStyle to the TableStyles Collection. This is from MSDN documentation:



CAUTION Always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection before adding DataGridTableStyle objects to the GridTableStylesCollection. When you add an empty DataGridTableStyle to the collection, DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection. Alternatively, clear the GridColumnStylesCollection using the Clear method.



And when you create the DataGridColumnStyles(normally either DataGridBoolColumn or DataGridTextBoxColumn) make sure you set the MappingName.


Try something like:

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();
column.MappingName = "Column1";

DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
.....

gridStyle.GridColumnStyles.Add(column);
gridStyle.GridColumnStyles.Add(column2);
.....

DataGrid1.TableStyles.Add(gridStyle);



Good luck,
Jacob


I have a datagrid on WinForm and I am trying to format the Grid.
I am using the following code, the code runs fine, but nothing happens after it runs, the datagrid does not change (with data in the grid or with out any data)

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";
DataGrid1.TableStyles.Add(gridStyle);

gridStyle.GridColumnStyles[0].Width = 100;
gridStyle.GridColumnStyles[0].HeaderText="Test Col";
gridStyle.DataGrid.Refresh();


What am I doing wrong?
 
P

Peter

Ok, I have the following code and nothing happens after I run it. What do I have to do to format / change the layout of the datagrid?

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();
column.MappingName = "Column1";

DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
gridStyle.GridColumnStyles.Add(column);
gridStyle.GridColumnStyles.Add(column2);
dataGrid1.TableStyles.Add(gridStyle);

I believe you need to create some DataGridColumnStyle objects and add them to the DataGridTableStyle BEFORE you add the DataGridTableStyle to the TableStyles Collection. This is from MSDN documentation:



CAUTION Always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection before adding DataGridTableStyle objects to the GridTableStylesCollection. When you add an empty DataGridTableStyle to the collection, DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection. Alternatively, clear the GridColumnStylesCollection using the Clear method.



And when you create the DataGridColumnStyles(normally either DataGridBoolColumn or DataGridTextBoxColumn) make sure you set the MappingName.


Try something like:

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();
column.MappingName = "Column1";

DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
....

gridStyle.GridColumnStyles.Add(column);
gridStyle.GridColumnStyles.Add(column2);
....

DataGrid1.TableStyles.Add(gridStyle);



Good luck,
Jacob


I have a datagrid on WinForm and I am trying to format the Grid.
I am using the following code, the code runs fine, but nothing happens after it runs, the datagrid does not change (with data in the grid or with out any data)

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";
DataGrid1.TableStyles.Add(gridStyle);

gridStyle.GridColumnStyles[0].Width = 100;
gridStyle.GridColumnStyles[0].HeaderText="Test Col";
gridStyle.DataGrid.Refresh();


What am I doing wrong?
 
P

Peter

I figured it out. I was not supplying the table name in the DataSet
I was using this code
da.Fill(ds);

instead of

da.Fill(ds, srcTable);
Ok, I have the following code and nothing happens after I run it. What do I have to do to format / change the layout of the datagrid?

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();
column.MappingName = "Column1";

DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
gridStyle.GridColumnStyles.Add(column);
gridStyle.GridColumnStyles.Add(column2);
dataGrid1.TableStyles.Add(gridStyle);

I believe you need to create some DataGridColumnStyle objects and add them to the DataGridTableStyle BEFORE you add the DataGridTableStyle to the TableStyles Collection. This is from MSDN documentation:



CAUTION Always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection before adding DataGridTableStyle objects to the GridTableStylesCollection. When you add an empty DataGridTableStyle to the collection, DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection. Alternatively, clear the GridColumnStylesCollection using the Clear method.



And when you create the DataGridColumnStyles(normally either DataGridBoolColumn or DataGridTextBoxColumn) make sure you set the MappingName.


Try something like:

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";

DataGridTextBoxColumn column = new DataGridTextBoxColumn();
column.MappingName = "Column1";

DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
....

gridStyle.GridColumnStyles.Add(column);
gridStyle.GridColumnStyles.Add(column2);
....

DataGrid1.TableStyles.Add(gridStyle);



Good luck,
Jacob


I have a datagrid on WinForm and I am trying to format the Grid.
I am using the following code, the code runs fine, but nothing happens after it runs, the datagrid does not change (with data in the grid or with out any data)

DataGridTableStyle gridStyle = new DataGridTableStyle();
gridStyle.MappingName = "Test";
DataGrid1.TableStyles.Add(gridStyle);

gridStyle.GridColumnStyles[0].Width = 100;
gridStyle.GridColumnStyles[0].HeaderText="Test Col";
gridStyle.DataGrid.Refresh();


What am I doing wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top