gridview仅在运行时添加一些行

时间:2016-04-27 07:00:07

标签: c# asp.net gridview

我创建了一个可编辑的gridview,我想在运行时添加尽可能多的行。它工作正常,但我只能添加9行,之后我得到一个索引超出范围的异常,因为我没有在我的代码中的任何位置设置限制。

private void SetInitialRow()
{
    DataTable dtable = new DataTable();
    DataRow dr = null;
    dtable.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column1", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column2", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column3", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column4", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column5", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column6", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column7", typeof(string)));
    dtable.Columns.Add(new DataColumn("Column8", typeof(string)));

    dr = dtable.NewRow();
    dr["RowNumber"] = 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dr["Column4"] = string.Empty;
    dr["Column5"] = string.Empty;
    dr["Column6"] = string.Empty;
    dr["Column7"] = string.Empty;
    dr["Column8"] = string.Empty;

    dtable.Rows.Add(dr);

    //Store the DataTable in ViewState
    ViewState["CurrentTable"] = dtable;

    GridView1.DataSource = dtable;
    GridView1.DataBind();
}

 private void AddNewRowToGrid()
{
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {

            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
                TextBox Gtb_ffcode = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("Gtb_ffcode");
                TextBox Gtb_terr = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("Gtb_terr");
                DropDownList GddlTeam = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("GddlTeam");
                TextBox Gtb_fromdt = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("Gtb_fromdt");
                TextBox Gtb_todt = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("Gtb_todt");
                DropDownList Gddl_desg = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("Gddl_desg");
                DropDownList Gddl_role = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("Gddl_role");
                TextBox Gtb_manager = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("Gtb_manager");

                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = i + 1;

                dtCurrentTable.Rows[i - 1]["Column1"] = Gtb_ffcode.Text;
                dtCurrentTable.Rows[i - 1]["Column2"] = Gtb_terr.Text;
                dtCurrentTable.Rows[i - 1]["Column3"] = GddlTeam.Text;
                dtCurrentTable.Rows[i - 1]["Column4"] = Gtb_fromdt.Text;
                dtCurrentTable.Rows[i - 1]["Column5"] = Gtb_todt.Text;
                dtCurrentTable.Rows[i - 1]["Column6"] = Gddl_desg.Text;
                dtCurrentTable.Rows[i - 1]["Column7"] = Gddl_role.Text;
                dtCurrentTable.Rows[i - 1]["Column8"] = Gtb_manager.Text;

                drCurrentRow["Column1"] = Gtb_ffcode.Text = maxcode; // write ff code to the new generating row
                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);

            ViewState["CurrentTable"] = dtCurrentTable;
            GridView1.DataSource = dtCurrentTable;
            GridView1.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    //Set Previous Data on Postbacks
    SetPreviousData();
}

private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TextBox Gtb_ffcode = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("Gtb_ffcode");
                TextBox Gtb_terr = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("Gtb_terr");
                DropDownList GddlTeam = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("GddlTeam");
                TextBox Gtb_fromdt = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("Gtb_fromdt");
                TextBox Gtb_todt = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("Gtb_todt");
                DropDownList Gddl_desg = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("Gddl_desg");
                DropDownList Gddl_role = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("Gddl_role");
                TextBox Gtb_manager = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("Gtb_manager");

                Gtb_ffcode.Text = dt.Rows[i]["Column1"].ToString();
                Gtb_terr.Text = dt.Rows[i]["Column2"].ToString();
                GddlTeam.Text = dt.Rows[i]["Column3"].ToString();
                Gtb_fromdt.Text = dt.Rows[i]["Column4"].ToString();
                Gtb_todt.Text = dt.Rows[i]["Column5"].ToString();
                Gddl_desg.Text = dt.Rows[i]["Column6"].ToString();
                Gddl_role.Text = dt.Rows[i]["Column7"].ToString();
                Gtb_manager.Text = dt.Rows[i]["Column8"].ToString();

                rowIndex++;
            }
        }
    }
}

0 个答案:

没有答案