Help with DataGridTableStyle?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I have the following DataGridTableStyle:

DataGridTableStyle gridTableStyle = new DataGridTableStyle();
gridTableStyle.MappingName = "myStyle";
DataGridTextBoxColumn dgsc = new DataGridTextBoxColumn();
dgsc.MappingName = "FirstName";
dgsc.HeaderText = "First Name";
dgsc.Width = 30;
dgsc.TextBox.Enabled = false;
gridTableStyle.GridColumnStyles.Add(dgsc);

mygrid.TableStyles.Add(gridTableStyle);
mygrid.DataSource = someDataView;

I can see in the grid for the assigned table that the firstname column
is "FirstName". That should match. Instead, every row from the
DataSet shows in the grid. Shouldn't only the firstname column
display? What am I doing wrong?

Thanks,
Brett
 
Brett,

I don't think that you need to set the MappingName property on the table
style. The mapping name is for when you are binding to a specific type, not
for the name of the table, I believe.

From the documentation for the MappingName property:

To bind the DataGrid to a strongly typed array of objects, the object type
must contain public properties. To create a DataGridTableStyle that displays
the array, set the DataGridTableStyle.MappingName property to typename where
typename is replaced by the name of the object type. Also note that the
MappingName property is case-sensitive; the type name must be matched
exactly. See the MappingName property for an example.

Hope this helps.
 
Actually, I just needed to change this
gridTableStyle.MappingName = "myStyle";

to the actual table name coming from the DataSource.

Brett
 
/*You Need to Add only the required Column to a DataTable and then bind
that to the grid. */

dgsc.ColumnName = "FirstName";
/*Then you should add this Column to the Binding table*/
DataTable SomeDataTable = new DataTable();
SomeDataTable.Columns.Add(dgsc);

/*then */

mygrid.DataSource = SomeDataTable;


HTH
 
Brett,
You can suppress the display of a column with (example)

ds.Tables("Products").Columns("QuantityPerUnit").ColumnMapping =
MappingType.Hidden

--Peter
 
Back
Top