【datagrid】动态加载列 ...

       之前我们的项目在前台显示只需要把数据从数据库读出来进行显示就可以,datagrid的表头字段都是写死的,把数据往表里一扔,就基本没什么事儿了,结果客户前几天要求,其中一个字段不能是死的,应该是有多少项显示多少项,比如说,原来只需要显示:其他项总分,现在需要显示的则是:xx加分,xx加分,xx减分,xx加分。。。。字段不固定,有多少项也不确定,需要从数据库中查到相应的字段来进行显示。


       不能要求客户来适应咱们的系统啊,而应该全心全意为客户着想,所以,开始改。原来的情况是,所有的字段都是固定的,而且要显示的字段是跟数据库中某张表对应的,只需要查出来就可以。现在的情况是,其中一部分字段是固定的,另一部分字段是不固定的,然后固定字段的数据是从原来的表中查出来的,不固定字段的数据是从另外一张表查出来的。


       经过一下午和一晚上的努力,终于初步完成了显示效果,但是还有缺点就是没办法做到分页显示,现在的效果还需要进一步优化,先把当前完成的部分记录一下。


       代码如下:

       view端:

//datagrid动态加载列
        function getjson() 
            {
            $.getJSON('/QueryScores/QueryOtherAssess', null, function (otherscores) {
                var columns = new Array();
                var column2 = {
                    field: '教工号', title: '教工号', width: 50
                }
                columns.push(column2);
                var column3 = { field: '教职工姓名', title: '教职工姓名' }
                columns.push(column3);
                var column4 = { field: '工作效率', title: '工作效率', width: 50 }
                columns.push(column4);
                var column5 = { field: '职业道德', title: '职业道德', width: 50 }
                columns.push(column5);
                var column6 = { field: '业务能力', title: '业务能力', width: 50 }
                columns.push(column6);
                var column7 = { field: '廉洁自律', title: '廉洁自律', width: 50 }
                columns.push(column7);
                var column8 = { field: '工作成绩', title: '工作成绩', width: 50 }
                columns.push(column8);
                //导入其他分值的循环
                for (var i = 0; i < otherscores.sum; i++) {
                    var column1={
                        field:otherscores.OtherScoresAssess[i],title:otherscores.OtherScoresAssess[i],width:70
                    }
                    columns.push(column1);
                }
                var column9 = { field: '总分', title: '总分', width: 50 }
                columns.push(column9);
                initTable(columns);
            })
        }


        function initTable(columns) {
            $('#tt').datagrid({
                title: '查看成绩',
                url: '/QueryScores/QueryScoresIndex',
                width: '100%',
                rownumbers: true,
                columns: [
                    columns
                ]
            });
        }

view端调了controller端两个方法,QueryOtherAssess是为了读取到需要动态加载的列的表头字段。QueryScoresIndex是将数据查到返回的方法。

QueryOtherAssess:

public ActionResult QueryOtherAssess()
        {
            IList<string> OtherScoresAssess = OtherScoresAsscssProgramBLL.LoadEnities(u => u.IsUsed == true).Select(u => u.AsscssProgram).ToArray();

            var OtherScoresAssessList = new
            {
                sum = OtherScoresAssess.Count(),
                OtherScoresAssess = OtherScoresAssess
            };

            return Json(OtherScoresAssessList, JsonRequestBehavior.AllowGet);

        }
QueryScoresIndex首先将数据查出来,然后转成json字符串返回前台。

具体查数据就不贴了,看一下赋值的段落:

 //表头
            DataTable FinalTable = new DataTable();
            FinalTable.Columns.Add("教工号", typeof(int));
            FinalTable.Columns.Add("教职工姓名", typeof(string));
            FinalTable.Columns.Add("工作效率", typeof(string));
            FinalTable.Columns.Add("职业道德", typeof(string));
            FinalTable.Columns.Add("业务能力", typeof(string));
            FinalTable.Columns.Add("廉洁自律", typeof(string));
            FinalTable.Columns.Add("工作成绩", typeof(string));
            for (int i = 0; i < YzOtherProgramEntity.Count; i++)
            {
                FinalTable.Columns.Add(YzOtherProgramEntity[i].AsscssProgram, typeof(string));
            }
            FinalTable.Columns.Add("总分", typeof(string));
然后是赋值:

 DataRow FileRow = FinalTable.NewRow();
                    FileRow["教工号"] = scoresstaffid;
                    FileRow["教职工姓名"] = staffname;
                    FileRow["工作效率"] = staffscoresEntity[i].WorkEfficiency;
                    FileRow["职业道德"] = staffscoresEntity[i].ProfessionalEthics;
                    FileRow["业务能力"] = staffscoresEntity[i].BusinessAbility;
                    FileRow["廉洁自律"] = staffscoresEntity[i].HonestyDiscipline;
                    FileRow["工作成绩"] = staffscoresEntity[i].WorkPerformance;
                    
                    if (YzOtherProgramEntity != null)
                    {
                        for (int j = 0; j < YzOtherProgramEntity.Count; j++)
                        {
                            Decimal othertotalscores = 0;
                            Guid otherprogramEntityID = YzOtherProgramEntity[j].ID;
                            IList<YzOtherScoresEntity> YzOtherScoresEntity = OtherScoresBLL.LoadEnities(u => u.CriticID == staffguid && u.Program == otherprogramEntityID && u.IsUsed == true).ToArray();

                            for (int k = 0; k < YzOtherScoresEntity.Count; k++)
                            {
                                othertotalscores = othertotalscores + YzOtherScoresEntity[k].Number;
                            }

                            FileRow[YzOtherProgramEntity[j].AsscssProgram] = othertotalscores;

                        }



                        FileRow["总分"] = staffscoresEntity[i].TotalScores;
                        FinalTable.Rows.Add(FileRow);

最后转json字符串:

char[] specialChars = new char[] { ',' };
            string JSONstring = "[";

            int index = 0;
            foreach (DataRow dr in FinalTable.Rows)
            {
                JSONstring += "{";

                foreach (DataColumn dc in FinalTable.Columns)
                {
                    JSONstring += "\"" + dc.ColumnName + "\":\"" + dr[dc].ToString() + "\",";
                }
                JSONstring = JSONstring.TrimEnd(specialChars);
                JSONstring += "},";

                index++;
            }
            JSONstring = JSONstring.TrimEnd(specialChars);
            JSONstring += "]";
            return JSONstring;

然后前台进行接收,最后就可以做到动态加载列啦

结果:




转载于:https://www.cnblogs.com/zhemeban/p/7183125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF DataGrid 动态指的是在运行时根据数据源的变化来动态添加或移除。 在WPF中,可以使用DataGrid控件来展示数据。通常,我们可以在XAML中使用静态定义的来显示数据。然而,有时我们需要根据数据的不同特性来动态创建。 要实现动态的WPF DataGrid,我们需要在代码中处理数据源的变化并相应地动态添加或删除。首先,我们需要确定数据源的类型及结构,并监听数据源的变化。当数据源发生变化时,我们可以清空DataGrid集合,然后重新创建并添加新的动态创建需要以下步骤: 1. 监听数据源的变化,例如使用ObservableCollection作为数据源,并注册其CollectionChanged事件。 2. 在事件处理程序中,判断变化的类型,如果是添加或删除操作,则清空DataGrid集合。 3. 遍历数据源的每个元素,获取其属性信息,并创建对应的DataGrid。 4. 将创建的添加到DataGrid集合中。 在动态创建时,可以根据数据源的属性信息来确定的类型、宽度、标题等。例如,可以根据数据源的属性类型来选择不同的类型,如文本、复选框或日期等。 需要注意的是,动态的性能可能受到影响,特别是在数据源包含大量数据或频繁发生变化时。因此,我们应该谨慎使用动态,并根据实际需求进行性能优化。一种优化方法是使用虚拟化技术,仅加载可见的行和,以提升性能。 总而言之,通过在代码中监听数据源的变化,我们可以实现WPF DataGrid动态。这种方法允许我们根据数据源的变化来自动创建或删除,以适应不同的数据展示需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值