Generate Columns Dynamically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a datagrid that will display data from the database. Each user will
specify list of columns he wants to see and in the order he wants to see.
User A can specify that he wants to see only columns A,B,C and in the order
A,B,C and the user might say I want to see only only columns A,G,H but the
order should be
G,A,H. It's possible that column G might have a hyperlink.

How can I do it in Dot.Net? Any help will be appreciated.

Tahnks,
Andy
 
Hi Andy,

To dynamically add columns to a grid, you create DataGridColumnStyle objects
and add them to the GridColumnStyles collection of an object called a
TableStyle.

1. Drop a grid on a form
2. Add a TableStyle to the grid. For sake of example, lets say you name it
oTableStyle
3. When you know the name of the datatable you are binding the grid to, you
set the MappingName of oTableStyle to the name of the datatable
4. When you are adding the columns, you create a DataGridTextBoxColumn or
DataGridBoolColumn, set the MappingName to the name of the field in the
DataTable.
5. Add the columnstyle object to oTableStyle.GridColumnStyles collection.

oTableStyle.MappingName="MyTable"

Dim oColumn As DataGridTextBoxColumn

oColumn = New DataGridTextBoxColumn()
oColumn.MappingName = "Age"

oTableStyle.GridColumnStyles.Add(oColumn)

oColumn = New DataGridTextBoxColumn()
oColumn.MappingName = "ZipCode"

oTableStyle.GridColumnStyles.Add(oColumn)
 
Back
Top