【问题标题】:Creating row header in asp.net gridview在 asp.net gridview 中创建行标题
【发布时间】:2015-01-08 05:55:36
【问题描述】:

我在 asp.net 网格视图中有一个奇怪的要求。如下图可以同时有列标题和行标题吗?

如果是,我还有一个问题

1.) 我想根据 Failed QuantityLot Quantity 自动计算 Failure% 行中的失败百分比,就像我们在 excel 中所做的那样.

非常感谢您的帮助,在此先感谢。

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

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

    这就是我通常的做法,希望对你有所帮助

    【讨论】:

    • 谢谢@balanza。它确实有帮助,但如果行数增加,解决方案的长度往往会增加,这会引发对面向对象特性的质疑。是不是有一个网格视图属性或任何其他控件可以满足我的要求?
    • @user2470766 对于后处理单元格值,您始终可以使用GridView.RowDataBound 事件。但是对于处理数据表的结构,我只能建议使用中间转换集合,如上。 “如果行数增加,解决方案的长度往往会增加,这会引发对面向对象特性的质疑”,您是什么意思?
    • 在上述解决方案中,我们必须为每一行放入一个新集合。如果我的数据有数百行,那么对于每一行数据,我们必须再次编写代码的 newCollection.Add(new myRow() 部分。
    • 你不能用for循环来管理它吗?数据绑定现在怎么样了?
    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2013-07-12
    相关资源
    最近更新 更多