代码之家  ›  专栏  ›  技术社区  ›  iftee

如何在ASP.NET网格视图中添加行

  •  0
  • iftee  · 技术社区  · 17 年前

    到目前为止,我还不确定这是对还是错

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

    请帮忙

    2 回复  |  直到 14 年前
        1
  •  5
  •   John Sheehan    17 年前
    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();
    

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

        2
  •  0
  •   sjngm quinti    14 年前

    当创建新行时,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);
      }
    }
    
    推荐文章