| VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板列
using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Generic; /**//// <summary> /// /// </summary> public partial class qsa_ysap : System.Web.UI.Page { private int step; //接受传入参数 因本页自己链接自己,以此控制页面内容 private string kmbh; //接受传入参数 科目编号 private string unit_code; //接收传入参数 当前单位 private string fpro_id; //接收传入参数 预算编号 private List<String> col = new List<String>(); //用于存放各列的标记,此列存放单位编号 private List<String> row = new List<String>(); //用于存放各行的标记,此列存放科目编号 private List<String> colname = new List<String>(); private List<String> rowname = new List<String>();
protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["kmbh"] != null) { kmbh = Request.QueryString["kmbh"].ToString(); } else { kmbh = "0"; } if (Request.QueryString["step"] != null) { step = int.Parse(Request.QueryString["step"].ToString()); } else { step = 0; } if (Request.QueryString["unit_code"] != null) { unit_code = Request.QueryString["unit_code"].ToString(); } else { unit_code = "0001000000"; } if (Request.QueryString["fpro_id"] != null) { fpro_id = Request.QueryString["fpro_id"].ToString(); } else { fpro_id = "36a14788-df2c-41f1-947f-e7e2e53d92f9"; } InitGridView(); }
/**//// <summary> /// 初始化GridView /// </summary> private void InitGridView() {
Logic.BLL.T_YSKZ_BAS_ORG org = new Logic.BLL.T_YSKZ_BAS_ORG(); Logic.BLL.T_YSKZ_BAS_KMSZ kmsz = new Logic.BLL.T_YSKZ_BAS_KMSZ(); //单位表 DataTable dwt = org.GetList("f_org_fid='" + unit_code + "'").Tables[0]; //科目表 DataTable kmt = kmsz.GetList("f_kmbh='" + kmbh + "'").Tables[0];
//添加列头 DataTable dt = new DataTable(); dt.Clear(); dt.Columns.Clear(); dt.Columns.Add("科目"); for (int j = 0; j < dwt.Rows.Count; j++) { col.Add(dwt.Rows[j]["f_org_id"].ToString()); colname.Add(dwt.Rows[j]["f_org_name"].ToString());//.Substring(0, 2) dt.Columns.Add(dwt.Rows[j]["f_org_name"].ToString());//.Substring(0, 2) } dt.Columns.Add("合计");
//添加行数据 DataRow dr; for (int i = 0; i < kmt.Rows.Count; i++) { row.Add(kmt.Rows[i]["KMBH"].ToString());
|
2 | VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板 | | rowname.Add(kmt.Rows[i]["KNAME"].ToString()); dr = dt.NewRow(); dr["科目"] = kmt.Rows[i]["KNAME"].ToString();
double Vsum = 0; //纵向合计 for (int j = 0; j < dwt.Rows.Count; j++) { Vsum += GetCharge(dwt.Rows[j]["f_org_id"].ToString(), kmt.Rows[i]["KMBH"].ToString()); //.Substring(0, 2) dr[dwt.Rows[j]["f_org_name"].ToString()] = GetCharge(dwt.Rows[j]["f_org_id"].ToString(), kmt.Rows[i]["KMBH"].ToString()).ToString(); } dr["合计"] = Vsum.ToString(); dt.Rows.Add(dr);
//添加横向合计 if (i == kmt.Rows.Count - 1) { dr = dt.NewRow(); dr["科目"] = "合计:"; for (int j = 0; j < dwt.Rows.Count; j++) { //.Substring(0, 2) dr[dwt.Rows[j]["f_org_name"].ToString()] = GetChargeSumGroupByDept(dwt.Rows[j]["f_org_id"].ToString()).ToString(); } dr["合计"] = GetChargeSumGroupByDept(unit_code).ToString(); dt.Rows.Add(dr); } }
//动态绑定列 //GridView2.AutoGenerateColumns = false; //GridView2.ShowHeader = true; //GridView2.Columns.Clear();
//HyperLinkField hf = new HyperLinkField(); //hf.HeaderText = "科目"; //hf.DataTextField = "科目"; //GridView2.Columns.Add(hf);
//for (int i = 0; i < colname.Count; i++) //{ // hf = new HyperLinkField(); // hf.HeaderText = colname[i]; // hf.DataTextField = colname[i]; // hf.DataNavigateUrlFormatString = "a.aspx?unit_code="+col[i]+"&kmbh="+row[i]; // GridView2.Columns.Add(hf); //}
//BoundField bf = new BoundField(); //bf.HeaderText = "合计"; //bf.DataField = "合计"; GridView2.Columns.Clear(); GridView2.DataSource = dt; GridView2.DataBind(); }
/**//// <summary> /// 根据控件id,要生成控件的列动态生成模板列 /// </summary> /// <param name="id">模板列内控件的id</param> /// <param name="cellIndex">列的序号索引值</param> /// <param name="e">GridViewRowEventArgs</param> protected void ItemTemplateGenerate(string id, int cellIndex, GridViewRowEventArgs e) {
if (e.Row.RowType != DataControlRowType.Header) { int rowIndex = e.Row.RowIndex; //横向合计和弹出页面不加链接 if (rowIndex < row.Count && step == 0) { LinkButton lb = new LinkButton(); lb.ID = id; lb.BorderWidth = 0; lb.Text = e.Row.Cells[cellIndex].Text;
//不同列加不同的客户端脚本 if (rowIndex >= 0) { string script; if (cellIndex == 0) {
|
|
3 | VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板 | | script = "window.showModalDialog('ysap.aspx?unit_code=" + unit_code + "&fpro_id=" + fpro_id + "&kmbh=" + row[rowIndex] + "&step=1','_blank','" + string.Format("dialogWidth={0}px,dialogHeight={1}px,help:no;unadorned:yes;resizable:yes;status:no;location:location;", 1000, 768) + "')"; } else { lb.ToolTip = "aaaaaaa bnbbbbbb"; script = "window.showModalDialog('ysap.aspx?unit_code=" + col[cellIndex - 1] + "&fpro_id=" + fpro_id + "&kmbh=" + row[rowIndex] + "&step=1','_blank','" + string.Format("dialogWidth={0}px,dialogHeight={1}px,help:no;unadorned:yes;resizable:yes;status:no;location:location;", 1000, 768) + "')"; } lb.OnClientClick = script; } e.Row.Cells[cellIndex].Controls.Add(lb); } } else { if (step == 0 && cellIndex >= 1 && cellIndex < col.Count + 1) { LinkButton lb = new LinkButton(); //lb.ID = id; lb.BorderWidth = 0; lb.Text = e.Row.Cells[cellIndex].Text;
//不同列加不同的客户端脚本 string script; script = "window.showModalDialog('ysap.aspx?unit_code=" + col[cellIndex - 1] + "&fpro_id=" + fpro_id + "&kmbh=" + "0" + "&step=1','_blank','" + string.Format("dialogWidth={0}px,dialogHeight={1}px,help:no;unadorned:yes;resizable:yes;status:no;location:location;", 1000, 768) + "')"; lb.OnClientClick = script; e.Row.Cells[cellIndex].Controls.Add(lb); } }
}
/**//// <summary> /// 通过下级单位编号和科目编号获取费用 /// </summary> /// <param name="child_unit_code"></param> /// <param name="kmbh"></param> /// <returns></returns> private double GetCharge(string child_unit_code, string kmbh) { return 0; }
/**//// <summary> /// 获取单位的所有科目费用之和 /// </summary> /// <param name="child_unit_code"></param> /// <param name="kmbh"></param> /// <returns></returns> private double GetChargeSumGroupByDept(string child_unit_code) { return 0; }
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { //动态生成科目列 ItemTemplateGenerate("LB0", 0, e); //动态生成单位列 for (int i = 0; i < col.Count; i++) { ItemTemplateGenerate("LB" + (i + 1).ToString(), i + 1, e); } } }
|
|
|