Datagrid and Columns

  • Thread starter Thread starter Wayne
  • Start date Start date
W

Wayne

if I have a Collection, and I assign it to the DataSource Property of
a Datagrid, the data grid shows all the public properties of the items
in the collection.

How do I:
1) only show what I want shown, without making properites
non-public
2) give the columns differnt captions than the property name
3) put the columns in the order I want

This is for a windows form app, not a web app.

Thanks
Wayne
 
Hi Wayne,

You can use the TableStyles property of the DataGrid. Basically you create
a DataGridTableStyle object, set the MappingName to the name of your table,
then create a column object for each column you want to display. Add the
column objects to your datagridtablestyle object and then add the
datagridtablestyle object to your tablestyles property. Here is an example:

DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "tblFileLocator";

DataGridColumnStyle dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "Rec ID";
dgcs.MappingName = "RecID";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "System Name";
dgcs.MappingName = "SystemName";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "Last Name";
dgcs.MappingName = "LName";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "First Name";
dgcs.MappingName = "FName";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "SSNo";
dgcs.MappingName = "SSNo";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "Loan Number";
dgcs.MappingName = "LoanNo";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "Off Site Number";
dgcs.MappingName = "StorBoxNo";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "Entry Date";
dgcs.MappingName = "EntryDate";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);

this.GridResults.TableStyles.Add(dgts);
this.GridResults.DataSource = dsFileLocatorData;
this.GridResults.DataMember = "tblFileLocator";
 
See thats where my problem is, I don't have a Datasource. I am using
the Databinding to bind an Array or collection to a DataGrid. I've
tried what you have listed below, but don't seem to be able to make it
work. All the examples I've been able to find on the web also show how
to manipulate the columns/rows with Data sources, but not
Arrays/collections.

For a test I've created a new Project, added a datagrid to the form
and put the follwoing in the onload of the form:

Form[] forms = {this, this};
dataGrid1.DataSource = forms;

This succesfully shows all the public properties of the Form (this) in
the grid twice. However I would like to be able to control what gets
displayed. Any Ideas?

Thanks
Wayne
 
I got this problem before and was able to find out the solution.

All you need to do is to create a DataGridTableStyle, make SURE the
mappingname of this style to be the same as your List Type.
e.g. If you have a class named as ItemCollection, which inherits from
ArrayList, you should set the mappingName as "ItemCollection". The rest of
the settings for the DataGridTableStyle is the same as when you have a
DataSet as datasource.

Hope that helps.

Calvin

Wayne said:
See thats where my problem is, I don't have a Datasource. I am using
the Databinding to bind an Array or collection to a DataGrid. I've
tried what you have listed below, but don't seem to be able to make it
work. All the examples I've been able to find on the web also show how
to manipulate the columns/rows with Data sources, but not
Arrays/collections.

For a test I've created a new Project, added a datagrid to the form
and put the follwoing in the onload of the form:

Form[] forms = {this, this};
dataGrid1.DataSource = forms;

This succesfully shows all the public properties of the Form (this) in
the grid twice. However I would like to be able to control what gets
displayed. Any Ideas?

Thanks
Wayne


"Frank Wisniewski" <[email protected]> wrote in message
Hi Wayne,

You can use the TableStyles property of the DataGrid. Basically you create
a DataGridTableStyle object, set the MappingName to the name of your table,
then create a column object for each column you want to display. Add the
column objects to your datagridtablestyle object and then add the
datagridtablestyle object to your tablestyles property. Here is an example:

DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "tblFileLocator";

DataGridColumnStyle dgcs = new DataGridTextBoxColumn();
dgcs.HeaderText = "Rec ID";
dgcs.MappingName = "RecID";
dgcs.Width = 100;
dgcs.ReadOnly = true;
dgcs.Alignment = HorizontalAlignment.Left;
dgts.GridColumnStyles.Add(dgcs);
 
Back
Top