DataRow: Select cell value by a given column name

后端 未结 8 1538
小鲜肉
小鲜肉 2020-12-15 15:19

I have a problem with a DataRow that I\'m really struggling with.

The datarow is read in from an Excel spreadsheet using an OleDbConnection.

If I try to sele

8条回答
  •  半阙折子戏
    2020-12-15 15:40

    Which version of .NET are you using? Since .NET 3.5, there's an assembly System.Data.DataSetExtensions, which contains various useful extensions for dataTables, dataRows and the like.

    You can try using

    row.Field("fieldName");
    

    if that doesn't work, you can do this:

    DataTable table = new DataTable();
    var myColumn = table.Columns.Cast().SingleOrDefault(col => col.ColumnName == "myColumnName");
    if (myColumn != null)
    {
        // just some roww
        var tableRow = table.AsEnumerable().First();
        var myData = tableRow.Field(myColumn);
        // or if above does not work
        myData = tableRow.Field(table.Columns.IndexOf(myColumn));
    }
    

提交回复
热议问题