Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Swapping columns in a DataGrid

Reply
Thread Tools

Swapping columns in a DataGrid

 
 
Tim
Guest
Posts: n/a
 
      04-16-2004
Hi,

The DataGrid.Bind() below works fine as is. Now, I want to swap the
1st and 2nd columns of the DataGrid (see the commented out section):

DataSet ds = new DataSet();
ds.Tables.Add(this.GetTable(dr));
DataGrid1.DataSource = ds.Tables[0];

//DataGridColumn dgCol1;
//DataGridColumn dgCol2;
//int tryme = Convert.ToInt32(DataGrid1.Columns.Count);
//Response.Write(tryme); //returns zero
//dgCol1 = DataGrid1.Columns[0];
//dgCol2 = DataGrid1.Columns[1];
//DataGrid1.Columns.AddAt(0,dgCol2);
//DataGrid1.Columns.AddAt(1,dgCol1);

DataGrid1.DataBind();

However, DataGrid1.Columns.Count returns zero. How come? A DataGrid
with 8 columns is displayed normally.

Thanks!
 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      04-16-2004
Tim wrote:
> //DataGridColumn dgCol1;
> //DataGridColumn dgCol2;
> //int tryme = Convert.ToInt32(DataGrid1.Columns.Count);
> //Response.Write(tryme); //returns zero
> //dgCol1 = DataGrid1.Columns[0];
> //dgCol2 = DataGrid1.Columns[1];
> //DataGrid1.Columns.AddAt(0,dgCol2);
> //DataGrid1.Columns.AddAt(1,dgCol1);
>
> DataGrid1.DataBind();
>
> However, DataGrid1.Columns.Count returns zero. How come? A DataGrid
> with 8 columns is displayed normally.


Tim, the problem you are having is because the DataGrid's Columns
collection is not build up until *after* the data is bound to the grid
(that is, after the call to the DataBind() method).

Why can't you just reorder the BoundColumns in the <Columns> section, or
(if you're using AutoGenerateColumns=False), why can't you just
reorder the fields in your SQL query?

--

Scott Mitchell

http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
swapping the rows and columns shrikanthraon General Computer Support 0 08-14-2009 04:19 PM
Swapping of columns and rows in gridview shrikanthraon ASP .Net 0 08-09-2009 09:05 AM
Swapping table columns VA Javascript 10 11-07-2005 08:34 PM
better way of swapping columns and rows in a table (code provided) foldface@yahoo.co.uk Javascript 1 09-08-2004 07:36 AM
swapping columns on ItemDataBoundEventHandler Tim ASP .Net Datagrid Control 0 04-21-2004 06:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57