How to add New Column to existing DataTable in specific position.

Discussion in 'ASP .Net Datagrid Control' started by GoodMorningSky, Oct 5, 2004.

  1. I want to add new DataColumn to existing DataTable object as first column
    shown in the datagrid.
    (like
    DataColumn dc = new DataColumn("Target");
    dc.DataType = typeof(string);
    dc.DefaultValue = tbl.TableName;
    tbl.Columns.Add(dc);
    )
    Whenever I add it, the column is shown at the end.
    How can I add it as first column (or with index)?
    GoodMorningSky, Oct 5, 2004
    #1
    1. Advertising

  2. Does the order of the data columns really matter? You can determine the
    order of the columns when you design the grid.

    "GoodMorningSky" <> wrote in message
    news:uxjS$...
    >I want to add new DataColumn to existing DataTable object as first column
    > shown in the datagrid.
    > (like
    > DataColumn dc = new DataColumn("Target");
    > dc.DataType = typeof(string);
    > dc.DefaultValue = tbl.TableName;
    > tbl.Columns.Add(dc);
    > )
    > Whenever I add it, the column is shown at the end.
    > How can I add it as first column (or with index)?
    >
    >
    Ken Cox [Microsoft MVP], Oct 5, 2004
    #2
    1. Advertising

  3. GoodMorningSky

    Alex Homer Guest

    Alex Homer, Oct 5, 2004
    #3
  4. Ken Cox [Microsoft MVP], Oct 5, 2004
    #4
  5. GoodMorningSky

    Alex Homer Guest

    You mean you're supposed to read the question as well? <g>

    I guess one way would be to build a new DataTable by adding the new column
    first and then using the
    DataColumnCollection.AddRange Method
    to copy the others, but that seems hard work. Better to just specify the
    columns in the required order in the control that displays the rows (which
    is where I was going with the DataGrid...)

    .... or change the query that fills the DataTable?

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > Hmmm. That adds to the datagrid but he asked about the DataTable
    >
    > "Alex Homer" <> wrote in message
    > news:...
    > > DataGridColumnCollection.AddAt Method
    > >

    http://msdn.microsoft.com/library/d...lsdatagridcolumncollectionclassaddattopic.asp
    > >
    > >

    >
    Alex Homer, Oct 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jg
    Replies:
    1
    Views:
    720
    =?Utf-8?B?U2hhaWxlc2ggSmFubnU=?=
    Aug 17, 2004
  2. Ersin Gençtürk
    Replies:
    1
    Views:
    3,477
    Ersin Gençtürk
    Oct 6, 2004
  3. TB
    Replies:
    2
    Views:
    6,344
  4. janetb
    Replies:
    1
    Views:
    1,297
    janetb
    Feb 8, 2010
  5. Deep Mehta via .NET 247
    Replies:
    2
    Views:
    416
    Dave A
    May 31, 2005
Loading...

Share This Page