2009-08-14 67 views
0

到目前为止,我已经这样做了添加行,我不知道这是否是对还是错如何在asp.net网格视图

public partial class _Default : System.Web.UI.Page 
{ 
    Label l = new Label(); 
    GridView gv = new GridView(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     for (int i = 0; i < 5; i++) 
     { 
      GridViewRow gvr = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal); 
      gvr.Controls.Add(l); 
      gv. (what to do here) 
     } 

     this.Controls.Add(gv); 

    } 
} 

请帮助

回答

5
gv.Rows.Add(gvr); 

如果”重新用空的GridView,开始一个更简单的方法来动态地创建x行是创建一个虚设列表,然后将其设置到数据源:

var list = new List<string>(10); // replace 10 with number of empty rows you want 
// for loop to add X items to the list 
gv.DataSource = list; 
gv.DataBind(); 

如果你这样做,我建议用Repeater做。管理起来要容易得多。

0

DataGrid在创建新行时触发RowCreate事件。 折叠

//OnRowCreated="GridView3_RowCreated" 

protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e) 
{ 

    //When a child checkbox is unchecked then the header checkbox will also be unchecked. 
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == 
    DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)) 
    { 

    CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkselect"); 
    CheckBox chkBxHeader = (CheckBox)this.GridView3.HeaderRow.FindControl("chkHeader"); 
    chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(
     this,'{0}');", chkBxHeader.ClientID); 
    } 
}