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(); 
         }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2021-09-13
  • 2021-10-25
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-27
  • 2022-12-23
  • 2022-01-02
  • 2021-09-05
  • 2021-12-16
  • 2022-12-23
  • 2021-08-17
相关资源
相似解决方案