Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrange Columns in Datagrid 1

Status
Not open for further replies.

Coolstep

Programmer
Dec 7, 2004
50
EG
Dear all,
In my project I currently do the following:
1. Fill a datatable from DB
2. Have a DataGridTableStyle mapped with the datatable to hide some columns
3. the DataGridTableStyle is assigned to a datagrid

Everything works fine except that I can't arrange the columns, I'm forced to use the order of columns in the SQL Statement that filled the DataTable.

Any advice on how to order the columns?
 
you have to add TextBoxColumn objects to a GridColumnStyles collection, and attach this to the TableStyle object of the datagrid:

Code:
this.<datagrid>.TableStyles.Clear();
DataGridTableStyle myTableStyle = new DataGridTableStyle();
myTableStyle.MappingName = <dataset>.<datatable>.TableName;
myTableStyle.GridColumnStyles.Clear();

[green]//**************************************************[/green]
[green]// for each column you want to display[/green]
[green]// add the following commands:[/green]
DataGridTextBoxColumn myTextBoxColumn = new DataGridTextBoxColumn();
[green]// this is the name of the column from the sql[/green]
myTextBoxColumn.MappingName = "<COLUMN_NAME>";
[green]// optionally, formatting goes here: there are lots of options[/green]
myTextBoxColumn.HeaderText = "Header";
myTextBoxColumn.Alignment = HorizontalAlignment.Right;
myTextBoxColumn.Format = "#,##0";
[green]// this add the column to the grid[/green]
myTableStyle.GridColumnStyles.Add(myTextBoxColumn);
[green]//**************************************************[/green]

[green]// after you've defined the columns:[/green]
this.<datagrid>.TableStyles.Add(myTableStyle);

hope this helps,

mr s. <;)

 
MisterStick,

I appreciate your help, it worked perfect. Also I do appreciate ur organized and clear reply.

Thank You :)
 
many thanks for the star, much appreciated.

i forgot to say that his code goes in the Form_Load event handler, after the InitializeComponent statement.


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top