2015-01-08 53 views
0

我在asp.net网格视图中有这个奇怪的要求。是否可以同时显示列标题和行标题,如下图所示?在asp.net中创建行标题gridview

enter image description here

如果是我做的多了一个问题

1)我想根据失败的数量地块数量为我们自动计算不良率在失败%行在Excel中做。

帮助深表感谢,提前致谢。

回答

3

我想你有一个集合,我们称它为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 
}); 

您撰写newCollectionorigCollection从你的数据结构深深依赖的方式,无论如何,你可以将所有的过程包装在一个函数中,我们称之为transformCollection()。然后,数据绑定很简单:

var newcoll = transformCollection(origCollection); 
this.myGrid.DataSource = newcoll; 
this.myGrid.Databind(); 

这是我平时做的方式,我希望这种帮助

+0

感谢@balanza。它确实有帮助,但是如果行数增加会导致问题出现在面向对象的特征上,那么解决方案的长度往往会增加。没有网格视图属性或任何其他控件可以满足我的要求吗? –

+0

@ user2470766对于后处理单元格值,您始终可以使用'GridView.RowDataBound'事件。但是为了处理数据表的结构,我只能建议使用一个中间转换集合,如上所述。 “如果行数增加会引起对象方向的特征问题,那么解决方案的长度会增长吗?” – balanza

+0

在上面的解决方案中,我们必须将每一行放入一个新的集合中。如果我的数据有数百行,那么对于每一行数据,我们必须再次编写newCollection.Add(new myRow()代码的一部分。 –