【发布时间】:2015-01-08 05:55:36
【问题描述】:
我在 asp.net 网格视图中有一个奇怪的要求。如下图可以同时有列标题和行标题吗?
如果是,我还有一个问题
1.) 我想根据 Failed Quantity 和 Lot Quantity 自动计算 Failure% 行中的失败百分比,就像我们在 excel 中所做的那样.
非常感谢您的帮助,在此先感谢。
【问题讨论】:
我在 asp.net 网格视图中有一个奇怪的要求。如下图可以同时有列标题和行标题吗?
如果是,我还有一个问题
1.) 我想根据 Failed Quantity 和 Lot Quantity 自动计算 Failure% 行中的失败百分比,就像我们在 excel 中所做的那样.
非常感谢您的帮助,在此先感谢。
【问题讨论】:
我猜你有一个集合,我们称它为origCollection,它以某种方式包含数据。我通常做的是将origCollection转换成另一个,我们称之为newCollection,它有你想要的形式。你需要这个类来映射单行:
Class myRow {
public string title {get; set;} //Failure %, Failed Quantity, ...
public string test1{get; set;}
public string test2{get; set;}
public string test3{get; set;}
public string test4{get; set;}
public string test5{get; set;}
}
然后,在数据绑定之前,您可以这样创建newCollection:
//create newCollection
var newCollection = new List<myRow>();
//for each row you wnt to put in newCollection:
newCollection.Add(new myRow(){
title = "Failure %",
test1 = // calculate value from origCollection,
test2 = // calculate value from origCollection,
test3 = // calculate value from origCollection,
test4 = // calculate value from origCollection,
test5 = // calculate value from origCollection
} );
从origCollection 组合newCollection 的方式很大程度上取决于您的数据结构,无论如何您可以将所有过程包装在一个函数中,我们称之为transformCollection()。那么数据绑定就简单了:
var newcoll = transformCollection(origCollection);
this.myGrid.DataSource = newcoll;
this.myGrid.Databind();
这就是我通常的做法,希望对你有所帮助
【讨论】:
GridView.RowDataBound 事件。但是对于处理数据表的结构,我只能建议使用中间转换集合,如上。 “如果行数增加,解决方案的长度往往会增加,这会引发对面向对象特性的质疑”,您是什么意思?