none
How to reorder the data table column inside the for loop RRS feed

  • Question

  • Hi,

    How to reorder the data table column inside the loop. For example:

    Column1  |  Column2  |  Column3  |  Column4  |  Column5

    -------------------------------------------------------------------------

    I need to reorder like below:

    Column4  |  Column3  |  Column1  |  Column2  |  Column5

    -------------------------------------------------------------------------

    I tried below but below will not Reorder because it changes the order. But I need this inside the loop since I have many dynamic column based on the table selection. I can't hardcode directly:

     for (int dtInputCount = 0; dtInputCount < inpuDataTable.Columns.Count; dtInputCount++) {                                      

    if (inpuDataTable.Columns[dtInputCount].ColumnName == "Column1") {
    csvDataTablecopy.Columns[csvDataTable.Columns[i].ColumnName].SetOrdinal(2);
                    }
                    else if (inpuDataTable.Columns[dtInputCount].ColumnName == "Column2") {csvDataTablecopy.Columns[csvDataTable.Columns[i].ColumnName].SetOrdinal(4);
                    }
                    else if (inpuDataTable.Columns[dtInputCount].ColumnName == "Column3") {csvDataTablecopy.Columns[csvDataTable.Columns[i].ColumnName].SetOrdinal(1);
                    }
                    else if (inpuDataTable.Columns[dtInputCount].ColumnName == "Column4") {csvDataTablecopy.Columns[csvDataTable.Columns[i].ColumnName].SetOrdinal(0);

                    }
                    else if (inpuDataTable.Columns[dtInputCount].ColumnName == "Column5") {csvDataTablecopy.Columns[csvDataTable.Columns[i].ColumnName].SetOrdinal(4);

                    }

    }


    Monday, July 22, 2013 1:10 PM

Answers

  • As you told there are many dynamic and unknown columns, Instead of reordering column in existing datatable, make use of new datatable and copy the from columns from the last to first.



    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, July 22, 2013 6:13 PM
  • Generate a new array to keep the name of the column you need to reorder,then loop the new array ,adjust the position of the column

     DataTable inpuDataTable=new DataTable();
    
               DataColumn second = new DataColumn("second", typeof(int));
               inpuDataTable.Columns.Add(second);
    
               DataColumn dc5 = new DataColumn("Column5", typeof(string));
               inpuDataTable.Columns.Add(dc5);
    
               DataColumn dc3 = new DataColumn("Column3", typeof(string));
               inpuDataTable.Columns.Add(dc3);
    
               DataColumn dc1 = new DataColumn("Column1", typeof(string));
               inpuDataTable.Columns.Add(dc1);
    
               DataColumn first = new DataColumn("first", typeof(int));
               inpuDataTable.Columns.Add(first);
    
               DataColumn dc2 = new DataColumn("Column2", typeof(string));
               inpuDataTable.Columns.Add(dc2);
    
               DataColumn dc4 = new DataColumn("Column4", typeof(string));
               inpuDataTable.Columns.Add(dc4);
                //a new array keep the columns you need to reorder
               string[] columnsname = new string[] { "Column1", "Column2", "Column3", "Column4", "Column5" };
    
    
               for (int dtInputCount  = 0; dtInputCount  < columnsname.Length; dtInputCount ++)
               {
                   inpuDataTable.Columns[columnsname[dtInputCount]].SetOrdinal(dtInputCount);
               }

    Tuesday, July 23, 2013 4:41 AM

All replies

  • As you told there are many dynamic and unknown columns, Instead of reordering column in existing datatable, make use of new datatable and copy the from columns from the last to first.



    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, July 22, 2013 6:13 PM
  • Generate a new array to keep the name of the column you need to reorder,then loop the new array ,adjust the position of the column

     DataTable inpuDataTable=new DataTable();
    
               DataColumn second = new DataColumn("second", typeof(int));
               inpuDataTable.Columns.Add(second);
    
               DataColumn dc5 = new DataColumn("Column5", typeof(string));
               inpuDataTable.Columns.Add(dc5);
    
               DataColumn dc3 = new DataColumn("Column3", typeof(string));
               inpuDataTable.Columns.Add(dc3);
    
               DataColumn dc1 = new DataColumn("Column1", typeof(string));
               inpuDataTable.Columns.Add(dc1);
    
               DataColumn first = new DataColumn("first", typeof(int));
               inpuDataTable.Columns.Add(first);
    
               DataColumn dc2 = new DataColumn("Column2", typeof(string));
               inpuDataTable.Columns.Add(dc2);
    
               DataColumn dc4 = new DataColumn("Column4", typeof(string));
               inpuDataTable.Columns.Add(dc4);
                //a new array keep the columns you need to reorder
               string[] columnsname = new string[] { "Column1", "Column2", "Column3", "Column4", "Column5" };
    
    
               for (int dtInputCount  = 0; dtInputCount  < columnsname.Length; dtInputCount ++)
               {
                   inpuDataTable.Columns[columnsname[dtInputCount]].SetOrdinal(dtInputCount);
               }

    Tuesday, July 23, 2013 4:41 AM