none
Adding new column to start of datatable? RRS feed

  • Question

  •  

    Is it possible to add a new column to the beginning of an existing datatable or re-order the columns perhaps?

     

    The code I use which adds to the end is:

     

    rejectDS.Tables(0).Columns.Add("Reject Message", GetType(System.String))

     

     

    The scenario is that I have loaded a data table by importrow from another data table where the rows have been 'rejected'.

    I need to display a 'rejected reason' against each row - I want this message to be displayed at the start of a datagridview which has the datatable as its datasource (not at the end which is currently happening). The reject reason is not on the database it is purely for display.

     

     

     

     

     

    Tuesday, December 2, 2008 4:47 PM

Answers

  • Yes, you can change the ordinal position of a DataColumn using the SetOrdinal method.

     

    Code Snippet

    using System;
    using System.Data;

    public class T {
     public static void Main() {
      try {
       DataTable table = new DataTable();
       table.Columns.Add("A");
       table.Columns.Add("B");
       table.Columns.Add("C");
     
       Console.WriteLine("before");
       DataColumn rejectReason = table.Columns.Add("Reject Message", typeof(String));
       foreach(DataColumn column in table.Columns) {
         Console.WriteLine("{0}: {1}", column.Ordinal, column.ColumnName);
       }
      
       Console.WriteLine("after");
       rejectReason.SetOrdinal(0);
       foreach(DataColumn column in table.Columns) {
         Console.WriteLine("{0}: {1}", column.Ordinal, column.ColumnName);
       }
      } catch(Exception e) { Console.WriteLine(e); }
     }
    }

     

     

    The output of the code snippet looks like this

    before
    0: A
    1: B
    2: C
    3: Reject Message
    after
    0: Reject Message
    1: A
    2: B
    3: C


    Tuesday, December 2, 2008 9:41 PM
    Moderator

All replies

  • Yes, you can change the ordinal position of a DataColumn using the SetOrdinal method.

     

    Code Snippet

    using System;
    using System.Data;

    public class T {
     public static void Main() {
      try {
       DataTable table = new DataTable();
       table.Columns.Add("A");
       table.Columns.Add("B");
       table.Columns.Add("C");
     
       Console.WriteLine("before");
       DataColumn rejectReason = table.Columns.Add("Reject Message", typeof(String));
       foreach(DataColumn column in table.Columns) {
         Console.WriteLine("{0}: {1}", column.Ordinal, column.ColumnName);
       }
      
       Console.WriteLine("after");
       rejectReason.SetOrdinal(0);
       foreach(DataColumn column in table.Columns) {
         Console.WriteLine("{0}: {1}", column.Ordinal, column.ColumnName);
       }
      } catch(Exception e) { Console.WriteLine(e); }
     }
    }

     

     

    The output of the code snippet looks like this

    before
    0: A
    1: B
    2: C
    3: Reject Message
    after
    0: Reject Message
    1: A
    2: B
    3: C


    Tuesday, December 2, 2008 9:41 PM
    Moderator
  • Excellent - thanks very much

     

    Wednesday, December 3, 2008 9:37 AM