Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Reorder gridview's columns

Reorder gridview's columns

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tee
    wrote on last edited by
    #1

    I try to reorder column of a gridview by overriding CreateColumns. The problem is after i reorder the column, the result table is not correct. The data that is bound into the result table doesn't arrange as i aspect, only the header is. Here is the example .- no reorder H1 H2 H3 D11 D12 D13 D12 D 22 D23 reorder column H2 H3 H1 D11 D12 D13 D12 D 22 D23 why is it ?

    M 1 Reply Last reply
    0
    • T Tee

      I try to reorder column of a gridview by overriding CreateColumns. The problem is after i reorder the column, the result table is not correct. The data that is bound into the result table doesn't arrange as i aspect, only the header is. Here is the example .- no reorder H1 H2 H3 D11 D12 D13 D12 D 22 D23 reorder column H2 H3 H1 D11 D12 D13 D12 D 22 D23 why is it ?

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Assuming the GridView control have more than 2 fields, the sample code below changes the order of the first two colomns:

      protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
      {
      ICollection collection = base.CreateColumns(dataSource, useDataSource);

      DataControlField\[\] array = new DataControlField\[collection.Count\];
      collection.CopyTo(array, 0);
      DataControlField tempField = array\[0\];
      array\[0\] = array\[1\];
      array\[1\] = tempField;
                 
      return array;
      

      }

      T 1 Reply Last reply
      0
      • M minhpc_bk

        Assuming the GridView control have more than 2 fields, the sample code below changes the order of the first two colomns:

        protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
        {
        ICollection collection = base.CreateColumns(dataSource, useDataSource);

        DataControlField\[\] array = new DataControlField\[collection.Count\];
        collection.CopyTo(array, 0);
        DataControlField tempField = array\[0\];
        array\[0\] = array\[1\];
        array\[1\] = tempField;
                   
        return array;
        

        }

        T Offline
        T Offline
        Tee
        wrote on last edited by
        #3

        Nope, this would not work at all; at least for my code. As I said on my question, i can reorder all the columns as expect, and i check that the ICollection that return from the CreateColumns is correct. But however, the data that is bound to the table is not correct. Actually, i already have the solution. I would post here if someone interest.

        M 1 Reply Last reply
        0
        • T Tee

          Nope, this would not work at all; at least for my code. As I said on my question, i can reorder all the columns as expect, and i check that the ICollection that return from the CreateColumns is correct. But however, the data that is bound to the table is not correct. Actually, i already have the solution. I would post here if someone interest.

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Tee+ wrote:

          Nope, this would not work at all; at least for my code

          Not sure what you do with your custom control, but it does work in my case when I simply declare 3 fields in the exGridView control and use a sqldatasource to feed data. Anyway, glad you solved your problem.

          T 1 Reply Last reply
          0
          • M minhpc_bk

            Tee+ wrote:

            Nope, this would not work at all; at least for my code

            Not sure what you do with your custom control, but it does work in my case when I simply declare 3 fields in the exGridView control and use a sqldatasource to feed data. Anyway, glad you solved your problem.

            T Offline
            T Offline
            Tee
            wrote on last edited by
            #5

            Oh yeah, this might be the cause. I use ObjectDataSource. And i think the GridView do a early bound and cache it something like [ColumnIndex, Value]. Therefore, even if i reorder all columns, but when doing a actual binding The grdiview doesn't use the datafield in each column to retrieve the data, but use the cache data and the column index. -- modified at 10:31 Saturday 19th August, 2006

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups