Create new winform datagrid

D

Dale Williams

Everyone:

Using C#, I'm trying to create a datagrid on a winform programatically.
I placed the datagrid and the form and named it CHGrid. Here's the
code I'm using:

// Get and bind the data.
QarBusTier.DefectFoundLocation DFL = new
QarBusTier.DefectFoundLocation();
System.Data.DataTable DFLTable = DFL.LoadAll();

CHGrid.DataSource = DFLTable;

// Set the data grid style.
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Customers";

// Set the datagrid style.
CHGrid.Top = 50;
CHGrid.Left = 200;
CHGrid.Width = 350;
CHGrid.Height= 200;
CHGrid.ForeColor = System.Drawing.Color.Black;
CHGrid.BackColor = System.Drawing.Color.Beige;

// Create column.
DataGridBoolColumn myDataCol = new DataGridBoolColumn();
myDataCol.MappingName = "DefectFoundLocationId";
myDataCol.HeaderText = "My New Column";
myDataCol.Width = 100;
ts1.GridColumnStyles.Add(myDataCol);

CHGrid.TableStyles.Add(ts1);

The DFL.LoadAll() just gets all the data from a table. My grid is
populated with the 4 columns being returned from DFL.LoadAll() but the
column I create is not being displayed.

I want to display only the column(s) I create and not have the grid
auto-populate.

Thanks for the help,
Dale Williams
 
D

Dmytro Lapshyn [MVP]

Hi Dale,

Define all the column and table styles FIRST and bind the grid to the data
source SECOND (I'd also recommend that you do the binding with the grid's
SetDataBinding method.
 

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