Swapping columns in a DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Tim, Apr 16, 2004.

  1. Tim

    Tim Guest

    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!
    Tim, Apr 16, 2004
    #1
    1. Advertising

  2. 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!
    Scott Mitchell [MVP], Apr 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shrikanthraon

    Swapping of columns and rows in gridview

    shrikanthraon, Aug 9, 2009, in forum: .NET
    Replies:
    0
    Views:
    1,516
    shrikanthraon
    Aug 9, 2009
  2. rob thomson
    Replies:
    0
    Views:
    241
    rob thomson
    Sep 4, 2003
  3. Tim

    swapping columns on ItemDataBoundEventHandler

    Tim, Apr 21, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    107
  4. Replies:
    1
    Views:
    100
  5. VA

    Swapping table columns

    VA, Nov 1, 2005, in forum: Javascript
    Replies:
    10
    Views:
    190
Loading...

Share This Page