That didn't work for me as the TableStyles
collection of the DataGrid
control was empty at the point you specify and remains so until a DataGridTableStyle
collection is added.
Using your suggestion for setting the correct value for the MappingName
property, I achieved the desired result by creating and adding a new DataGridTableStyle
object containing only the public fields that were required in the DataGrid
.
// Create a DataGridTableStyle to hold all the columns to be displayed in the DataGrid
DataGridTableStyle myTableStyle = new DataGridTableStyle();
myTableStyle.MappingName = myBindingSource.GetListName(null); // This is the magic line
myTableStyle.GridColumnStyles.Clear();
// Add some DataGridColumnStyles
DataGridTextBoxColumn columnRowId = new DataGridTextBoxColumn();
columnRowId.MappingName = "idx"; //This must match the name of the public property
ColumnRowId.HeaderText = "Record";
tableStyleReportsSummary.GridColumnStyles.Add(columnRowId);
// Add the table style to the DataGrid
myDataGrid.TableStyles.Clear();
myDataGrid.TableStyles.Add(myTableStyle);