将值绑定到GridView

时间:2012-04-10 11:39:59

标签: asp.net gridview

我有一个包含以下值的列表

 List<Calculations> calcs = new List<Calculations>();
 Calculations cal = new Calculations();
    cal.TotalTotalC2 = Convert.ToInt32(reader["TotalTotalC2"].ToString());
     cal.TotalTotalC3 = Convert.ToInt32(reader["TotalTotalC3"].ToString());
     cal.TotalC1C4IOM = Convert.ToInt32(reader["TotalC1C4IOM"].ToString());
    cal.TotalC1C4MDR = Convert.ToInt32(reader["TotalC1C4MDR"].ToString());
calcs.Add(cal);

我需要将这些数值绑定在下表格式中。左侧手边纯文本和右侧边界值。

enter image description here

有人可以告诉我如何使用GridView以上述格式进行绑定。

2 个答案:

答案 0 :(得分:0)

你必须在RowDataBound事件中编写代码,并在每次迭代中检查行号并根据行号将自定义列表的列文本绑定到列,我认为没有任何自动函数可以绑定列名到网格!!!如果你能找到直接和容易的东西,请告诉我

答案 1 :(得分:0)

我必须动态生成DataTable,然后将其绑定到GridView

   DataTable dt=new DataTable();
    dt.Columns.Add("Title", typeof(string));
    dt.Columns.Add("Count", typeof(int));

 foreach (Calculations item in calcs )
        {
    dt.Rows.Add("Total ...", item.TotalTotalC2);
    dt.Rows.Add("Total ...", item.TotalTotalC3);
}