I am able to Bind my DataGrid in .NET 3.5 CF to a List() but I am unable to format the columns by specifying their width. Below is the code that looks like it should work but does not. I am pretty sure that I am not setting the MappingName correctly as all tutorials tell you to set it to the name of your DataTable but I am not binding to a DataTable so I am not quiet sure what to do.
grdBatch.DataSource = InventoryItems;
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = InventoryItems.ToString();
DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
tbcName.Width = 400;
tbcName.MappingName = "SERIAL_ID";
tbcName.HeaderText = "SERIAL_ID";
tableStyle.GridColumnStyles.Add(tbcName);
grdBatch.TableStyles.Clear();
grdBatch.TableStyles.Add(tableStyle);
grdBatch is a DataGrid and InventoryItems is a List of POCOS(Plain old C# Objects).
At run time, use the SetDataBinding method to set the DataSource and DataMember properties. The following data sources are valid: A DataTable.
The DataGrid control provides a flexible way to display a collection of data in rows and columns. The DataGrid includes built-in column types and a template column for hosting custom content. The built-in row type includes a drop-down details section that you can use to display additional content below the cell values.
The DataGridView control supports the standard Windows Forms data binding model, so it can bind to a variety of data sources. Usually, you bind to a BindingSource that manages the interaction with the data source.
Change:
tableStyle.MappingName = InventoryItems.ToString();
to
tableStyle.MappingName = InventoryItems.GetType().Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With