locked
GridView Dyanmically binding columns. RRS feed

  • Question

  •  

    Hello All,

     

    We are using GridView control and we would like to generate all columns of grid in predefine order. It means we want to decide order of column at runtime (When page load). We also required sorting with up-down image on sort column and paging functionality of Gridview.

    it is possible with GridView ?

    So please help us.

    Thanks.

    Sanjay Patel



    Sanjay Patel
    Tuesday, May 31, 2011 11:04 AM

Answers

  • There is no easy solution for rearrange. All you need to delete the column and reinser on RowCreated. Ex:
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
     {
    
        GridViewRow row = e.Row;
        // Intitialize TableCell list
        List<TableCell> columns = new List<TableCell>();
        foreach (DataControlField column in GridView1.Columns)
        {
          //Get the first Cell /Column
          TableCell cell = row.Cells[0];
          // Then Remove it after
          row.Cells.Remove(cell);
          //And Add it to the List Collections
          columns.Add(cell);
        }
    
        // Add cells
        row.Cells.AddRange(columns.ToArray());
      }
    
     

    Thanks
    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Tuesday, May 31, 2011 11:46 AM

All replies

  • We are using  Grid control and we would like to generate all columns of grid in predefine order. It means we want to decide order of column at runtime (When page load). We also required sorting with up-down image on sort column and paging functionality of Grid.

    So please help us.


    Sanjay Patel
    • Merged by Larcolais Gong Thursday, June 2, 2011 9:18 AM duplicated question
    Tuesday, May 31, 2011 11:10 AM
  • Dynamic Gridview Sample

    http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx

    Custom Paging & Sorting Sample

    http://www.aspdotnetcodes.com/Custom_Sorting_Paging_GridView.aspx

     


    Please "Mark as Answer" if this post answered your question. :)

    Kalpesh Chhatrala | Software Developer | Rajkot | India

    Kalpesh 's Blog

    VFP Form to C#, Vb.Net Conversion Utility

    Tuesday, May 31, 2011 11:19 AM
  • 1) For column rearrange, best solution is using Autogenerated columns for GridView. Rearrange your data columns and bind the grid will automatically rearrange grid columns.

    2) Sorting and Paging functionality is already there in GridView.

    Thanks

     


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Tuesday, May 31, 2011 11:25 AM
  • Hello Kalpesh,

     

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/f3de3c28-55b5-45b5-87e3-648032f2a4ca

     

    same link of my thead,

     

    give me you griview sample link.

     

    Thanks.


    Sanjay Patel
    Tuesday, May 31, 2011 11:25 AM
  • Sorry By mistake wronge Paste

    Check below link

    http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx


    Please "Mark as Answer" if this post answered your question. :)

    Kalpesh Chhatrala | Software Developer | Rajkot | India

    Kalpesh 's Blog

    VFP Form to C#, Vb.Net Conversion Utility
    Tuesday, May 31, 2011 11:28 AM
  • Hello Anirban,

    thanks for reply,

    i can't use Autogenerated= "true" becoz of that are lot's of formating and business logic on data after getting from data from dB.

    any other idea ?

    Thanks.

    Sanjay Patel


    Sanjay Patel
    Tuesday, May 31, 2011 11:30 AM
  • Hello Kalpesh,

     

    thanks for reply, but i want use Template colums becoz's i have client and server side event.

     

    any other way ?


    Sanjay Patel
    Tuesday, May 31, 2011 11:40 AM
  • There is no easy solution for rearrange. All you need to delete the column and reinser on RowCreated. Ex:
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
     {
    
        GridViewRow row = e.Row;
        // Intitialize TableCell list
        List<TableCell> columns = new List<TableCell>();
        foreach (DataControlField column in GridView1.Columns)
        {
          //Get the first Cell /Column
          TableCell cell = row.Cells[0];
          // Then Remove it after
          row.Cells.Remove(cell);
          //And Add it to the List Collections
          columns.Add(cell);
        }
    
        // Add cells
        row.Cells.AddRange(columns.ToArray());
      }
    
     

    Thanks
    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Tuesday, May 31, 2011 11:46 AM