get index of DataTable column with name

145,411

Solution 1

You can use DataColumn.Ordinal to get the index of the column in the DataTable. So if you need the next column as mentioned use Column.Ordinal + 1:

row[row.Table.Columns["ColumnName"].Ordinal + 1] = someOtherValue;

Warning:

This code returns the next column, so the one after ColumnName, as requested in the question.

Solution 2

Try this:

int index = row.Table.Columns["ColumnName"].Ordinal;

Solution 3

You can simply use DataColumnCollection.IndexOf

So that you can get the index of the required column by name then use it with your row:

row[dt.Columns.IndexOf("ColumnName")] = columnValue;

Solution 4

I wrote an extension method of DataRow which gets me the object via the column name.

public static object Column(this DataRow source, string columnName)
{
    var c = source.Table.Columns[columnName];
    if (c != null)
    {
        return source.ItemArray[c.Ordinal];
    }

    throw new ObjectNotFoundException(string.Format("The column '{0}' was not found in this table", columnName));
}

And its called like this:

DataTable data = LoadDataTable();
foreach (DataRow row in data.Rows)
{        
    var obj = row.Column("YourColumnName");
    Console.WriteLine(obj);
}
Share:
145,411
Andy
Author by

Andy

Updated on July 09, 2022

Comments

  • Andy
    Andy almost 2 years

    I have some code which sets the value of cells in a DataRow by column name i.e.

    row["ColumnName"] = someValue;
    

    I want to also set the value for this row in the column immediately to the right of the one found above. Clearly if I was getting the cell by index rather than by column name this would be easy. So is there a way of getting the column index from the column name thus allowing me to do:

    row[index + 1] = someOtherValue;
    

    i.e. do I need create some kind of dictionary of column index and column names when the table is initially created, or can I get the index from the column name later on without doing this?

  • Andy
    Andy almost 12 years
    +1 although as Tim beat you to it I'll mark his as the answer.
  • Piers Myers
    Piers Myers almost 10 years
    You don't need the + 1 just row[row.Table.Columns["ColumnName"].Ordinal] = someOtherValue; will do.
  • Tim Schmelter
    Tim Schmelter almost 10 years
    @Piers: but OP needed the next column after another column.
  • Jonathan Kittell
    Jonathan Kittell over 9 years
    Should this extension throw "ObjectNotFoundException" instead of "ItemNotFoundException"?? msdn.microsoft.com/en-us/library/…
  • DeadlyChambers
    DeadlyChambers almost 9 years
    -1 DataRow has that already built in your example if you did row["YourColumnName"] it will return the content of that cell. If the column doesn't exist it throws System.ArguementException and this is the message {"Column 'YourColumnName' does not belong to table Table."} So unless this has some performance gain please delete. Also this doesn't answer the question of how to get the index.
  • C4d
    C4d about 8 years
    You should write ..to get the index of the NEXT column then. I accidently used your line in my code while doing a mistake cuz of that +1. Thanks @PiersMyers for noticing it.
  • thevinaychoudhary
    thevinaychoudhary almost 5 years
    @TimSchmelter, How to get all columns index in an array? example: Table.Columns[0,1,2,3,4,5,6,7]