我有这段代码返回一个数据表,现在我想在调用一个方法时必须在数据网格视图中显示该数据表

时间:2019-01-23 14:46:25

标签: xml visual-studio file

我希望此函数在调用时显示在数据网格视图中

private DataTable resetColumnsOrder(DataTable dataTable,string[] selectedQuarters)
{

   try
   {
       List<string> colNames = new List<string>();
       int startIndex = 0; int x = 0;
       string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>()
                               select dc.ColumnName).ToArray();
       foreach(string quarter in selectedQuarters)
       {
           if (quarter == "All")
               continue;
           if(Array.Exists<string>(columnNames, s => s.StartsWith(quarter + "¢")))
           {
               string firstE = Array.Find<string>(columnNames, s => s.StartsWith(quarter + "¢"));
               string [] qElements = Array.FindAll<string>(columnNames, s => s.StartsWith(quarter + "¢"));
               startIndex = Array.FindIndex<string>(columnNames, s => s == firstE);
               if (x == 0)
               {
                   for (int i = 0; i < startIndex; i++)
                   {
                       colNames.Add(columnNames[i]);
                   }
               }
               colNames.AddRange(qElements);
           }
           x++;
       }
       colNames.Add("Grand Total");
       dataTable.SetColumnsOrder(colNames.ToArray());
       dataTable.AcceptChanges();
   }
   catch (Exception ex)
   {
       LogErrorService.LogError("Error in method (resetColumnsOrder) - " + ex.Message.ToString());
   }
   return dataTable;
}

0 个答案:

没有答案