change order of Column in excel

4 posts, 0 answers
  1. Farshad heidary
    Farshad heidary avatar
    53 posts
    Member since:
    Oct 2009

    Posted 28 Jun 2010 Link to this post

    hi guys
    how ican change order of column in grid for expord.
    for example i have a grid with 3 column c1 c2 c3
    now i wanna show in excel c3 c1 c2
    how can i implement that?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Jun 2010 Link to this post

    Hello,

    Have you tried setting OrderIndex for the columns before exporting the grid?

    CS:
     
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            RadGrid1.MasterTableView.GetColumn("C2").OrderIndex = 0; 
            RadGrid1.MasterTableView.GetColumn("C1").OrderIndex = 1; 
            RadGrid1.MasterTableView.Rebind(); 
               . . . 
            RadGrid1.MasterTableView.ExportToExcel(); 
        } 


    -Shinu.
  3. Farshad heidary
    Farshad heidary avatar
    53 posts
    Member since:
    Oct 2009

    Posted 28 Jun 2010 Link to this post

    it doesn't work :(

  4. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 01 Jul 2010 Link to this post

    Hello Farshad,

    If you are using the ExcelML export, you could reorder the columns directly  in the ExcelMLExportRowCreated event handler::
    protected  void RadGrid1_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
    {
        ReorderColumn(2, 1, e.Row); //ReorderColumn(old index, new index, row object)
    }

    Copy Code
    private void ReorderColumn(int oldIndex, int newIndex, RowElement row)
    {
        if (oldIndex < newIndex)
        {
            for (int i = oldIndex; i < newIndex; i++)
            {
                var oldCell = row.Cells[i + 1];
                row.Cells[i + 1] = row.Cells[i];
                row.Cells[i] = oldCell;
            }
        }
        else if (oldIndex > newIndex)
        {
            for (int i = oldIndex; i > newIndex; i--)
            {
                var oldCell = row.Cells[i - 1];
                row.Cells[i - 1] = row.Cells[i];
                row.Cells[i] = oldCell;
            }
        }
      
        for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
            row.Cells[cellIndex].Attributes["ss:Index"] = (cellIndex + 1).ToString();
    }



    Kind regards,
    Daniel
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top