DataGrid后台绑定模板列

来源:互联网 发布:软件测试常见概念 编辑:程序博客网 时间:2024/04/29 10:09

        需求说明:在DataGrid中显示手机短信过滤类型(手机号码,业务推广短信,帐户变动短信,节日祝福短信),即手机号是否发送相应类型的短信。由于被过滤的短信类型可能会动态添加,因此绑定时只能通过后台动态绑定。接下来以把原需求简化后说明DataGrid动态绑定模板列的过程,下图是示例的显示效果。

        示例显示效果

       1.定义ColumnTemlate继承ITemplate,该类的实例可以赋值给模板列的ItemTemplate属性。ColumnTemlate的作用是在模板列中添加CheckBox控件,并根据绑定的字段设置CheckBox的Checked属性。

//ColumnTemplate 从ITemplate继承。
        
//InstantiateIn 定义子控件的属于谁
        public class ColumnTemplate:System.Web.UI.ITemplate
        
{
            
string id;
            
string bindField;

            
public ColumnTemplate(string id,string bindField)
            
{
                
this.id = id;
                
this.bindField = bindField; 
            }


            
//Override the ITemplate.InstantiateIn method to ensure 
            
//that the templates are created in a CheckBox control and
            
//that the CheckBox object's DataBinding event is associated
            
//with the BindData method.
            public void InstantiateIn(Control container)
            
{
                CheckBox ckb 
= new CheckBox();
                ckb.ID 
= this.id;
                ckb.DataBinding 
+= new EventHandler(this.BindData);
                container.Controls.Add(ckb);
            }


            
//Create a public method that will handle the
            
//DataBinding event called in the InstantiateIn method.
            public void BindData(object sender, EventArgs e)
            
{
                CheckBox ckb 
= (CheckBox) sender;
                DataGridItem container 
= (DataGridItem) ckb.NamingContainer;
                
string isFilter = ((DataRowView) container.DataItem)[bindField].ToString();  
                ckb.Checked 
= bool.Parse(isFilter);
            }

        }

 

 

      2.动态生成模板列的页面

public class TemplateColumnDemo : System.Web.UI.Page
        
{
            
protected System.Web.UI.WebControls.DataGrid dgSmsFilters;
        
            
private void Page_Load(object sender, System.EventArgs e)
            
{
                
// Put user code to initialize the page here
                if(!Page.IsPostBack)
                
{
                    
this.BindGrid();
                }

            }

            
            
Web Form Designer generated code

            
private DataTable GetDates()
            
{
                DataTable dtSmsFilters 
= new DataTable("dtSmsFilters");
                DataColumn dcSmsNum 
= new DataColumn("手机号",Type.GetType("System.String"));
                dtSmsFilters.Columns.Add(dcSmsNum);
                
//产生短信过滤类型,实际应根据数据库中内容动态生成的,列数不固定
                DataColumn dcSmsFilterType1 = new DataColumn("业务推广",Type.GetType("System.Boolean"));
                dtSmsFilters.Columns.Add(dcSmsFilterType1);
                DataColumn dcSmsFilterType2 
= new DataColumn("帐户变动",Type.GetType("System.Boolean"));
                dtSmsFilters.Columns.Add(dcSmsFilterType2);
                
//手动添加测试数据
                DataRow dr;
                
for(int i=0;i<10;i++)
                
{
                    
//赋值,无实际意义,只为显示
                    dr = dtSmsFilters.NewRow();
                    dr[
0= "1366666888"+i.ToString();
                    dr[
1= i%2==0?true:false;
                    dr[
2= i%3==0?true:false;
                    dtSmsFilters.Rows.Add(dr);
                }

                
return dtSmsFilters;
            }


            
private void BindGrid()
            
{
                DataTable dt 
= this.GetDates();
                
this.dgSmsFilters.DataSource = dt;

                
//添加绑定列,绑定手机号
                BoundColumn bc = new BoundColumn();
                bc.HeaderText 
= dt.Columns[0].ColumnName;
                bc.DataField 
= dt.Columns[0].ColumnName;
                
this.dgSmsFilters.Columns.AddAt(0,bc);

                
//添加模板列,绑定过滤短信类型
                for(int i=1;i<dt.Columns.Count;i++)
                
{
                    TemplateColumn tc 
= new TemplateColumn();
                    tc.HeaderText 
= dt.Columns[i].ColumnName;
                    tc.ItemTemplate 
= new ColumnTemplate("ckb"+i.ToString(),dt.Columns[i].ColumnName);
                    
this.dgSmsFilters.Columns.AddAt(i,tc);
                }

                
this.dgSmsFilters.DataBind();
            }

        }