DataColumn Name from DataRow (not DataTable)

后端 未结 4 1832
暗喜
暗喜 2020-12-30 18:23

I need to iterate the columnname and column datatype from a specific row. All of the examples I have seen have iterated an entire datatable. I want to pass a single row to a

相关标签:
4条回答
  • 2020-12-30 19:07

    You would still need to go through the DataTable class. But you can do so using your DataRow instance by using the Table property.

    foreach (DataColumn c in dr.Table.Columns)  //loop through the columns. 
    {
        MessageBox.Show(c.ColumnName);
    }
    
    0 讨论(0)
  • 2020-12-30 19:17

    You need something like this:

    foreach(DataColumn c in dr.Table.Columns)
    {
      MessageBox.Show(c.ColumnName);
    }
    
    0 讨论(0)
  • 2020-12-30 19:21

    You can make it easier in your code (if you're doing this a lot anyway) by using an extension on the DataRow object, like:

    static class Extensions
    {
        public static string GetColumn(this DataRow Row, int Ordinal)
        {
            return Row.Table.Columns[Ordinal].ColumnName;
        }
    }
    

    Then call it using:

    string MyColumnName = MyRow.GetColumn(5);
    
    0 讨论(0)
  • 2020-12-30 19:24

    use DataTable object instead:

     private void doMore(DataTable dt)
        {
        foreach(DataColumn dc in dt.Columns)
        {
        MessageBox.Show(dc.ColumnName);
        }
        }
    
    0 讨论(0)
提交回复
热议问题