【问题标题】:How to add the GridView Cells data into a two dimensional list and retrieve it如何将 GridView Cells 数据添加到二维列表中并检索它
【发布时间】:2012-12-18 05:06:43
【问题描述】:

我正在遍历 GridView 行和单元格以获取值并将它们存储在 2D array 中。 这是我的代码:

 for (int i = 0; i < GridView2.Rows.Count; i++)
 {
     for (int j = 0; j < (GridView2.Rows[i].Cells.Count - 3); j++)
     {
         // using the labels of the template fields.....
         Label values = (Label)GridView2.Rows[i].Cells[j].FindControl("Label" + j);
         GridValues[i,j] = values.Text; // gridvalues is my 2d array.    
     }
 }

我现在需要的是将从上述循环中获得的相同值添加到列表(2D)中。所以我创建了该列表的一个类

这是代码:

public  class GridValuesClass
{
    public string GridCustomerName { get; set; }
    public string GridPickUpPoint { get; set; }
    public DateTime GridPickUpDate { get; set; }
    public TimeSpan GridDropPoint { get; set; }
    public DateTime GridDropDate { get; set; }
    public string GridCabStatus { get; set; }
    public string GridAssignedCab { get; set; }
    public TimeSpan GridActualDropTime { get; set; }
}

这些属性代表网格视图中的列名。

那么,如何在这个类的 gridview 中添加值呢?

如果我的值存在于list&lt;GridValuesClass&gt; 中,我如何检索这些值

【问题讨论】:

    标签: list gridview collections multidimensional-array


    【解决方案1】:

    您无需将其存储在二维数组中,以便稍后存储在列表中。相反,您可以将其直接存储到列表中。

    有点像这样:

    var list = new List<GridValuesClass>();
    
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
         var job = new GridValuesClass {
              GridPickUpPoint = GridView2.Rows[i].Cells[0]...,
              GridPickUpDate = ...,
              // and so on with possible use of conversions to get the correct types
         }
         list.Add(job);
    }
    

    希望这会有所帮助。


    作为旁注,请使用更多有意义的名称。

    • 给这个类一个比GridValuesClass更有意义的类名,如果你有几个网格视图,那么这个名字就没有用了。从我收集到的关于你如何命名这些属性的信息来看,它看起来更像是出租车的工作订单。所以也许JobOrder?
    • GridView2 什么也没告诉我们。 gridview 包含什么样的数据?也许JobsGridView?
    • 是否需要在所有字段前加上Grid?如果在没有网格视图的环境中存储或使用数据怎么办?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2010-11-11
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多