• 关于GV和datatable动态绑定


     

    前台aspx页面

    <asp:GridView ID="GridView1" runat="server" Width="100%" SkinID="nviewSkinTotalStatistics"
    >
    <Columns>

    </Columns>
    </asp:GridView>

    后端.cs代码

    1.GV动态绑定

    /// <summary>
    /// 动态绑定GridView列
    /// </summary>
    void AddGridViewColumn()
    {
    GridView1.Columns.Clear();
    //第一列
    BoundField bfColumn0 = new BoundField();
    bfColumn0.DataField = "Month";
    bfColumn0.HeaderText = "月份";
    GridView1.Columns.Add(bfColumn0);

    //核医学试剂名称动态获取
    DataTable NuclearMedicineName = new DataTable();
    NuclearMedicineName = new RisTotalManage().GetNuclearMedicineName();
    IEnumerable<string> L_NuclearMedicineName = new List<string>();
    L_NuclearMedicineName = NuclearMedicineName.AsEnumerable().Select(t => t.Field<string>(Pub_dictionary.F_Dicvalue));

    foreach (var it in L_NuclearMedicineName)
    {
    BoundField bf = new BoundField();
    //表示绑定到该列的表的字段名
    bf.DataField = it;
    //表示该列的名称
    bf.HeaderText = it;
    GridView1.Columns.Add(bf);
    }

    //核素治疗量列
    BoundField bfColumnEnd = new BoundField();
    bfColumnEnd.DataField = "Counts";
    bfColumnEnd.HeaderText = "核素治疗量";
    GridView1.Columns.Add(bfColumnEnd);
    GridView1.Width = 12 * 60;
    GridView1.RowStyle.HorizontalAlign = HorizontalAlign.Center;
    }

    2.GV动态绑定之后动态生成一个和GV一样格式的datatable

    void AddStatisticsColumn(ref DataTable dtStatistics)
    {
    DataColumn dcClientDepart = new DataColumn("Month", typeof(string));
    dcClientDepart.Caption = "月份";
    dtStatistics.Columns.Add(dcClientDepart);

    //核医学试剂名称动态获取
    DataTable NuclearMedicineName = new DataTable();
    NuclearMedicineName = new RisTotalManage().GetNuclearMedicineName();
    IEnumerable<string> L_NuclearMedicineName = new List<string>();
    L_NuclearMedicineName = NuclearMedicineName.AsEnumerable().Select(t => t.Field<string>(Pub_dictionary.F_Dicvalue));

    foreach (var it in L_NuclearMedicineName)
    {

    DataColumn dcName = new DataColumn(it, typeof(string));
    dcName.Caption = it;
    dtStatistics.Columns.Add(dcName);
    }

    DataColumn dcCounts = new DataColumn("Counts", typeof(string));
    dcCounts.Caption = "核素治疗量";
    dtStatistics.Columns.Add(dcCounts);

    }

    3.填充datatable数据 最后赋值给GV完成操作

    DataTable dtStatistics = new DataTable();
    //构造一个和GV一样格式的datatable
    AddStatisticsColumn(ref dtStatistics); 第二步
    if (dtMaskulin == null) return null;
    if (dtMaskulin.Rows.Count == 0) return dtMaskulin;

    //所有试剂名称
    DataTable NuclearMedicineName = new DataTable();
    NuclearMedicineName = new RisTotalManage().GetNuclearMedicineName();
    IEnumerable<string> L_NuclearMedicineName = new List<string>();
    L_NuclearMedicineName = NuclearMedicineName.AsEnumerable().Select(t => t.Field<string>(Pub_dictionary.F_Dicvalue));

    //开始填充数据
    for (int i = 1; i <= 13; i++)
    {
    DataRow drNew1 = dtStatistics.NewRow();

    if (i == 13)
    {
    drNew1["Month"] = "总计";
    for (int j = 0; j < dtMaskulin.Rows.Count; j++)
    {
    drNew1[dtMaskulin.Rows[j]["contrastagentname"].ToString()] = drNew1[dtMaskulin.Rows[j]["contrastagentname"].ToString()] = dtMaskulin.Compute("sum(dosages)", string.Format(" contrastagentname='{0}'", dtMaskulin.Rows[j]["contrastagentname"]));
    }
    drNew1["Counts"] = dtMaskulin.Compute("sum(dosages)","");
    }
    else
    {
    drNew1["Month"] = i + "月";
    drNew1["Counts"] = dtMaskulin.Compute("sum(dosages)", string.Format("DATETIMES ='{0}'", sYear + "-" + i.ToString().getStr(2)));
    for (int j = 0; j < dtMaskulin.Rows.Count; j++)
    {
    drNew1[dtMaskulin.Rows[j]["contrastagentname"].ToString()] = dtMaskulin.Compute("sum(dosages)", string.Format(" contrastagentname='{0}' AND DATETIMES ='{1}'", dtMaskulin.Rows[j]["contrastagentname"],sYear + "-" + i.ToString().getStr(2)));
    }
    }

    dtStatistics.Rows.Add(drNew1);
    }

    return dtStatistics;

  • 相关阅读:
    用户登录
    open >>>文件操作
    随机数
    内置函数
    函数练习
    函数的动态参数
    函数的指定参数
    函数的普通参数
    函数的初步认识
    copy
  • 原文地址:https://www.cnblogs.com/sunlunhao/p/4651257.html
Copyright © 2020-2023  润新知