【问题标题】:Get the column index in Foreach loop over datarow在数据行的 Foreach 循环中获取列索引
【发布时间】:2018-04-14 15:11:19
【问题描述】:

在下面的 Foreach 循环中

foreach (DataRow row in dt.Rows)
{
   foreach (DataColumn col in dt.Columns)
   {
       var val=row[col].ToString()
       var index=??
   }
}

如何判断当前列是否为最后一列?

【问题讨论】:

  • 使用 for 循环或增加一个整数
  • dt.Columns.IndexOf(col)
  • if (dt.Columns.IndexOf(dt.Columns.Cast<DataColumn>().LastOrDefault()) == dt.Columns.IndexOf(col)) { }
  • 你想要什么?列索引,根据您的标题,还是检查当前列是否是最后一列(根据您的问题)?前一种情况由@Fabio 回答,后一种情况是它和dt.Columns.Count 的组合。

标签: c#


【解决方案1】:

如果我理解正确,你要找的是序数

    foreach (DataRow row in dt.Rows)
     {
       foreach (DataColumn col in dt.Columns)
         {
           var index = col.Ordinal+1; // Current column index
           if(index == dt.Columns.Count) // if column is last column in current row
              {
                your logice goes here
              }
          }
     }

它为您提供列项目的当前位置

检查这个MSDN Link

【讨论】:

    【解决方案2】:

    你也可以这样做:

    foreach (DataRow row in dt.Rows)
    {         
         for(int i=0; i< dt.Columns.Count; i++)
         {
            var val=row[i].ToString()
            // here i will be the index
            // dt.Columns[i] will be the column
         }
    }
    

    如果您的要求是对最后一列做某事[如问题中所述]意味着不需要继续迭代,您可以像这样使用列索引:row[dt.Columns.Count-1]

    【讨论】:

      【解决方案3】:

      这应该对你有帮助

      var index,val;
      foreach (DataRow row in dt.Rows)
      {
       foreach (DataColumn col in dt.Columns)
         {
            index=dt.Columns.IndexOf(col);
            val=row[index].ToString();
      
          if (index == dt.Columns.Count-1)
              //your code here...
         }
      }
      

      【讨论】:

        【解决方案4】:

        来试试这个。

        foreach (DataRow row in dt.Rows)
          {
           var index =0;
           foreach (DataColumn col in dt.Columns)
               {
                var val=row[col].ToString()
        
                  index++;
                  if(index == dt.Columns.Count)
                    //true
               }
          }
        

        【讨论】:

          【解决方案5】:

          使用下面的和平代码

          int total=dt.Columns.Count;
          int i=0;
          foreach (DataRow row in dt.Rows)
          {
          foreach (DataColumn col in dt.Columns)
             {
          if(i==total-1)
          

          {

          }

          i++;
              var val=row[col].ToString()
              var index=??
             }
          

          }

          【讨论】:

            【解决方案6】:
            public static void DataD() {
                    DataTable table = new DataTable();
                    table.Columns.Add("Dosage", typeof(int));
                    table.Columns.Add("Drug", typeof(string));
                    table.Columns.Add("Patient", typeof(string));
                    table.Columns.Add("Date", typeof(DateTime));
            
                    // Here we add five DataRows.
                    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
                    int count = table.Columns.Count;
                    var lastColumn = table.Columns[count - 1];
                    string columnname = lastColumn.ColumnName;
                    foreach (DataRow row in table.Rows)
                    {
                        foreach (DataColumn col in table.Columns)
                        {
                            var val = row[col].ToString();
                            var index = table.Columns.IndexOf(col);
                            if (columnname == col.ColumnName) {
                                // this is my last colums
                            }
                        }
                    }
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-09-12
              • 1970-01-01
              • 2013-07-18
              • 1970-01-01
              • 2016-03-24
              • 2010-11-29
              • 2020-02-25
              • 1970-01-01
              相关资源
              最近更新 更多