Jay
Jay

Reputation: 9582

How do I reorder the columns of a DataGrid?

How do I reorder the columns of a System.Windows.Forms.DataGrid?

Upvotes: 2

Views: 1767

Answers (3)

Enrico
Enrico

Reputation: 621

You can bind the column DisplayIndex to some integer in your datacontext.

<DataGridTextColumn Header="Name" DisplayIndex="{Binding Path=DisplayIndex_Name}" />

In code behind you can just change the value of the bound parameter, in this case DisplayIndex_Name and the bound DisplayIndex of the DataGridTextColumn will also change depending on what value you give it.

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcolumn.displayindex.aspx

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180788

By writing new data into the grid in the desired order.

Consider using a DataGridView instead. It has much better sorting capabilities.

Upvotes: 1

Damith
Damith

Reputation: 63065

If you bind a datatable then you can change column order as

dataTable.Columns["Column Name"].SetOrdinal(NewIndex);

You need to set DataSource null and clean the GridView before you bind it back

dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
dataGridView1.DataSource = dataTable;

Upvotes: 2

Related Questions