2009-08-14 101 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); 
    } 
}