Gridview动态绑定

InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif namespace Boyuanec.Utility
InBlock.gif{
InBlock.gif         public class MyTemplate : ITemplate
InBlock.gif        {
InBlock.gif
InBlock.gif                 private string strColumnName;
InBlock.gif
InBlock.gif                 private DataControlRowType dcrtColumnType;
InBlock.gif
InBlock.gif                 public MyTemplate( string strColumnName, DataControlRowType dcrtColumnType)
InBlock.gif                {
InBlock.gif
InBlock.gif                         this.strColumnName = strColumnName;
InBlock.gif
InBlock.gif                         this.dcrtColumnType = dcrtColumnType;
InBlock.gif
InBlock.gif                }
InBlock.gif
InBlock.gif                 public void InstantiateIn(Control ctlContainer)
InBlock.gif                {
InBlock.gif
InBlock.gif                         switch (dcrtColumnType)
InBlock.gif                        {
InBlock.gif
InBlock.gif                                 case DataControlRowType.Header: //列标题
InBlock.gif
InBlock.gif                                        Literal ltr = new Literal();
InBlock.gif
InBlock.gif                                        ltr.Text = strColumnName;
InBlock.gif
InBlock.gif                                        ctlContainer.Controls.Add(ltr);
InBlock.gif                                        
InBlock.gif                                        HtmlInputCheckBox chk = new HtmlInputCheckBox();
InBlock.gif
InBlock.gif                                        chk.ID = "chkError";
InBlock.gif
InBlock.gif                                        chk.Value = "";
InBlock.gif
InBlock.gif                                        chk.Attributes.Add( "onclick", "CheckAll('GVSF6',this)");
InBlock.gif
InBlock.gif                                        ctlContainer.Controls.Add(chk);
InBlock.gif
InBlock.gif                                         break;
InBlock.gif
InBlock.gif                                 case DataControlRowType.DataRow: //模版列内容
InBlock.gif
InBlock.gif                                        CheckBox chbError = new CheckBox();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                                        chbError.ID = "chbError";
InBlock.gif
InBlock.gif                                        chbError.Text = "";
InBlock.gif
InBlock.gif                                        chbError.DataBinding += new EventHandler(cbItem_DataBinding);
InBlock.gif
InBlock.gif                                        ctlContainer.Controls.Add(chbError);
InBlock.gif
InBlock.gif                                         break;
InBlock.gif                        }
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void cbItem_DataBinding( object sender, EventArgs e)
InBlock.gif                {
InBlock.gif                        CheckBox cbx = (CheckBox)sender;
InBlock.gif                         //取得GridViewRow(包含CheckBox控件)    
InBlock.gif                        
InBlock.gif                        GridViewRow row = (GridViewRow)cbx.NamingContainer;
InBlock.gif                         //进行数据绑定    
InBlock.gif
InBlock.gif                         //cbx.Checked = Convert.ToBoolean(DataBinder.Eval(row.DataItem, "WrongFlag"));
InBlock.gif                }
InBlock.gif
InBlock.gif        }
InBlock.gif}
调用
InBlock.gif ExchangeDate();
InBlock.gif                GVSF6.Columns.Clear();
InBlock.gif                Hashtable ht = GetTestPoint();
InBlock.gif                BoundField[] BoundFields = GetStaticBoundFields();
InBlock.gif                BoundField[] BoundFields1 = GetDynamicBoundFields(ht);
InBlock.gif                 if (BoundFields1.Length >0)
InBlock.gif                {
InBlock.gif                         for ( int i = 0; i < BoundFields.Length; i++)
InBlock.gif                        {
InBlock.gif                                GVSF6.Columns.Add(BoundFields[i]);
InBlock.gif                        }
InBlock.gif                         for ( int j = 0; j < BoundFields1.Length; j++)
InBlock.gif                        {
InBlock.gif                                GVSF6.Columns.Add(BoundFields1[j]);
InBlock.gif                        }
InBlock.gif                        TemplateField templateField = new TemplateField();
InBlock.gif                        templateField.HeaderTemplate = new MyTemplate( "全选", DataControlRowType.Header);
InBlock.gif                        templateField.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow);
InBlock.gif                        GVSF6.Columns.Add(templateField);
InBlock.gif                         try
InBlock.gif                        {
InBlock.gif                                DataSet ds = new BLLSF6().GetList(ddlCompany.SelectedValue, ddlPlant.SelectedValue, ddlLogicEquipment.SelectedValue, txtDateStart.Value, txtDateEnd.Value, Convert.ToInt32(ddlDataType.SelectedValue), GVSF6.PageSize, AspNetPager1.CurrentPageIndex, out recordCount);
InBlock.gif                                 this.GVSF6.DataSource = ds.Tables[0];
InBlock.gif                                 this.GVSF6.DataBind();
InBlock.gif                        }
InBlock.gif                         catch
InBlock.gif                        { }
InBlock.gif                }
InBlock.gif                 else
InBlock.gif                {
InBlock.gif                        ScriptManager.RegisterStartupScript(UpdatePanel3, GetType(), "msg", "alert('请选择测量项')", true);
InBlock.gif                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值