1 DataGrid本身的列或模板列用代码控制的绑定,只需在附加好数据源和DataBind()方法之间写就可以了,需要注意的是AutoGenerateColumns的属性要是false.
     eg:
        this.DataGrid1.DataSource =dvSouce;

        BoundColumn bc=new BoundColumn();
        bc.DataField="DECL_TITLE";
        bc.HeaderText="哈哈";
        bc.SortExpression =bc.DataField;
        this.DataGrid1.Columns.Add(bc);

        this.DataGrid1.DataBind();   

2 DataGrid中出现的控件或嵌套DataGrid的时候,动态绑定需要写在ItemDataBound的事件中.
   eg:this.DataGrid1.ItemCreated+=new DataGridItemEventHandler(DataGrid1_ItemCreated);
        private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {        

              if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem) 
              {  
                
 DataGrid dgrd1=(DataGrid)e.Item.FindControl("DataGrid2");

                   dgrd1.DataSource=((DataRowView)e.Item.DataItem).Row.GetChildRows("myrelation");

                    for(int i=0;i<=iYFLocate;i++)

                   {

                        BoundColumn dc=new BoundColumn();

                        dc.DataField=ds.Tables["Gz"].Columns[i].ColumnName;

                        dc.HeaderText=ds.Tables["Gz"].Columns[i].ColumnName;

                        dgrd1.Columns.Add(dc);

                   }

                   dgrd1.DataBind(); 
         }

posted on 2005-08-11 11:04  xiazhaoxia  阅读(1577)  评论(2编辑  收藏  举报