通过单击 asp.net 中的按钮在网格中添加多行

我需要通过单击按钮在 Gridview 中动态添加“n”行和“m”列(行包含 m 个文本框)。当用户点击按钮时,一个特定的整数值被初始化为一个变量,该变量是所需的行数。假设它的值为 8,因此已动态创建包含 m 个文本框的 8 行。

对于多行的功能,我只是添加了一些约束并注释了一些不需要的代码,如下代码所示:

 private void SetInitialRow()

    {

        DataTable dt = new DataTable();

        DataRow dr = null;

        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));

        dt.Columns.Add(new DataColumn("Column1", typeof(string)));

        dt.Columns.Add(new DataColumn("Column2", typeof(string)));

        dt.Columns.Add(new DataColumn("Column3", typeof(string)));

        dr = dt.NewRow();

        dr["RowNumber"] = 1;

        dr["Column1"] = string.Empty;

        dr["Column2"] = string.Empty;

        dr["Column3"] = string.Empty;

        dt.Rows.Add(dr);


        //Store the DataTable in ViewState

        ViewState["CurrentTable"] = dt;


        Gridview1.DataSource = dt;

        Gridview1.DataBind();

    }

    private void AddNewRowToGrid()

    {

        try

        {

            int j = 8, rowIndex = 0;


            //if (ViewState["CurrentTable"] != null)

            //{

            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)

            {

                for (int i = 1; i <= j; i++)

 调试后出现错误:索引超出范围。必须为非负数且小于集合的大小。


呼唤远方
浏览 124回答 2
2回答

手掌心

尝试这个&nbsp;private void SetInitialRow()&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataRow dr = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dt.Columns.Add(new DataColumn("Column1", typeof(string)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dt.Columns.Add(new DataColumn("Column2", typeof(string)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dt.Columns.Add(new DataColumn("Column3", typeof(string)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dr = dt.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dr["RowNumber"] = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dr["Column1"] = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dr["Column2"] = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dr["Column3"] = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dt.Rows.Add(dr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ViewState["CurrentTable"] = dt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Gridview1.DataSource = dt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Gridview1.DataBind();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;private void AddNewRowToGrid()&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int j = 8, rowIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataRow drCurrentRow = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (dtCurrentTable.Rows.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 1; i <= j; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//extract the TextBox values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox&nbsp; box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox&nbsp; box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox&nbsp; box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;drCurrentRow = dtCurrentTable.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;drCurrentRow["RowNumber"] = i + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dtCurrentTable.Rows.Add(drCurrentRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // dtCurrentTable.Rows.Add(drCurrentRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ViewState["CurrentTable"] = dtCurrentTable;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Gridview1.DataSource = dtCurrentTable;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Gridview1.DataBind();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;protected void Page_Load(object sender, EventArgs e)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!Page.IsPostBack)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetInitialRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;protected void ButtonAdd_Click(object sender, EventArgs e)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AddNewRowToGrid();&nbsp; &nbsp; &nbsp; &nbsp;}

互换的青春

希望这能解决您的问题页面加载&nbsp; protected void Page_Load(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!Page.IsPostBack)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; load();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }加载()protected void load()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;if (ViewState["CurrentData"] == null)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;DataTable dt = (DataTable)ViewState["CurrentData"];&nbsp; &nbsp; &nbsp; &nbsp;BindGrid(1);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }绑定网格()&nbsp;private void BindGrid(int rowcount)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow dr;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataColumn RcAccCode, RcAccAccount, RcAmount, RcAccId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int a = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RcAccCode = new DataColumn("RcAccCode", Type.GetType("System.String"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RcAccAccount = new DataColumn("RcAccAccount", Type.GetType("System.String"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RcAmount = new DataColumn("RcAmount", Type.GetType("System.String"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RcAccId = new DataColumn("RcAccId", Type.GetType("System.String"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(RcAccCode);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(RcAccAccount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(RcAmount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(RcAccId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBox TextBox1 = new TextBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBox TextBox2 = new TextBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBox TextBox3 = new TextBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ViewState["CurrentData"] != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt = (DataTable)ViewState["CurrentData"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dt.Rows.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr = dt.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[0] = dt.Rows[0][0].ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = dt.Rows.Count - 1; i >= 0; i--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow dr1 = dt.Rows[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = Convert.ToInt32(dr1["RcAccCode"].ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (temp == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr = dt.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[0] = a + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[1] = TextBox1.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[2] = TextBox2.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[3] = TextBox3.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(dr);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr = dt.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[0] =1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[1] = TextBox1.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[2] = TextBox2.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[3] = TextBox3.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(dr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If ViewState has a data then use the value as the DataSource&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ViewState["CurrentData"] != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gridview1.DataSource = (DataTable)ViewState["CurrentData"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gridview1.DataBind();&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Bind GridView with the initial data assocaited in the DataTable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gridview1.DataSource = dt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gridview1.DataBind();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Store the DataTable in ViewState to retain the values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewState["CurrentData"] = dt;&nbsp; &nbsp; }按钮点击事件&nbsp; &nbsp; protected void ButtonAdd_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = (DataTable)ViewState["CurrentData"];&nbsp; &nbsp; &nbsp; &nbsp; int count = dt.Rows.Count;&nbsp; &nbsp; &nbsp; &nbsp; BindGrid(count);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP