Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview? Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Go Back  Xtreme Visual Basic Talk > > > Change column order in a datatable or datagridview?


Reply
 
Thread Tools Display Modes
  #1  
Old 03-03-2008, 11:14 AM
Richard_U Richard_U is offline
Freshman
 
Join Date: Apr 2007
Posts: 46
Default Change column order in a datatable or datagridview?


Hi all.

I'm retooling an app, and condensing my queries down to one, but to do so, I would like to move the columns for better presentation.

I am reporting on errors for particlular equipment, and the user may want to get equipment by errors or errors by equipment.

Is there any way to change column order?
Reply With Quote
  #2  
Old 03-03-2008, 11:32 AM
Machaira's Avatar
MachairaChange column order in a datatable or datagridview? Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

You should just be able to change the order in which your SELECT clause is getting the columns.
Reply With Quote
  #3  
Old 03-04-2008, 02:43 PM
Richard_U Richard_U is offline
Freshman
 
Join Date: Apr 2007
Posts: 46
Default

<<You should just be able to change the order in which your SELECT clause is getting the columns.>>

That was not an option, as I was using the same query for three different datagridviews that needed to display the data in three different ways.

I managed to solve the problem.

What I did was as follows:

tbly.Columns(iColno).SetOrdinal(iOrd)
dg.DataSource = tbly

I fed the datagrid into a subroutine, and identified the column I needed to move based on the name of the datagrid. After moving the result set into a temporary table, I used the SetOrdinal method on the table and moved the required column to the required position.
Reply With Quote
  #4  
Old 04-04-2008, 12:13 PM
MeanDde MeanDde is offline
Newcomer
 
Join Date: Apr 2008
Posts: 2
Default

I was having the same issue. I found this link when I was snooping around:

http://www.levelextreme.net/ViewPage...507A6345413D3D

The link suggests the following code:

MyDataSet.Tables["MyTable"].Columns["MyColumn"].SetOrdinal(5);
DataSet dsNew = new DataSet();
DataTable dt = new DataTable("MyTableName");
DataColumn OldCol;

// Swapping columns 0 and 1
OldCol = MyDataSet.Tables[0].Columns[1];
dt.Columns.Add(new DataColumn(OldCol.ColumnName, OldCol.DataType));
OldCol = MyDataSet.Tables[0].Columns[0];
dt.Columns.Add(new DataColumn(OldCol.ColumnName, OldCol.DataType));

Haven't tested it (yet), but it sure looks impressive! << Grin >>
Reply With Quote
  #5  
Old 04-04-2008, 12:20 PM
MeanDde MeanDde is offline
Newcomer
 
Join Date: Apr 2008
Posts: 2
Default

Just tried it... Richard's way is MUCH simpler and worked like a champ.
Reply With Quote
  #6  
Old 06-03-2010, 12:31 PM
tddaniels tddaniels is offline
Newcomer
 
Join Date: Jun 2010
Posts: 1
Default

For future reference, I needed to do this with a DatGridView, again after the data was bound, so I just used to move the desired column to first column.

this.DataGridView.Columns[ndx].DisplayIndex = 0;

Hope this helps others.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview? Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview? Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
 
Change column order in a datatable or datagridview?
Change column order in a datatable or datagridview?
 
-->