簡體   English   中英

從ASP.NET GridView動態添加和刪除行

[英]Dynamically adding and deleting rows from ASP.NET GridView

我已經將以下來自網絡“ codeproject ”的代碼用於“從ASP.NET GridView動態添加和刪除行”,它使用了2個代碼“ AddNewRow”,“ SetPreviouseData”。 我在理解“ SetPreviouseData”的代碼時遇到問題,當定義時,似乎所有操作都將使用“ AddNewRow”代碼完成

grvStudentDetails.DataSource = dtCurrentTable;
grvStudentDetails.DataBind();

實際上,我之前不知道什么時候定義了數據源,但是“ SetPreviouseData”代碼的用途是什么。

    private void AddNewRow()
{
    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++)
            {
                TextBox TextBoxName = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
                TextBox TextBoxAge = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtAge");
                TextBox TextBoxAddress = 
                  (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtAddress");
                RadioButtonList RBLGender = 
                  (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
                DropDownList DrpQualification = 
                  (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("drpQualification");
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = i + 1;

                dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.Text;
                dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxAge.Text;
                dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxAddress.Text;
                dtCurrentTable.Rows[i - 1]["Col4"] = RBLGender.SelectedValue;
                dtCurrentTable.Rows[i - 1]["Col5"] = DrpQualification.SelectedValue;
                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            grvStudentDetails.DataSource = dtCurrentTable;
            grvStudentDetails.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }
    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 TextBoxName =  (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
                TextBox TextBoxAge = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtAge");
                TextBox TextBoxAddress = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtAddress");
                RadioButtonList RBLGender = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
                DropDownList DrpQualification = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("drpQualification");

                TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
                TextBoxAge.Text = dt.Rows[i]["Col2"].ToString();
                TextBoxAddress.Text = dt.Rows[i]["Col3"].ToString();
                RBLGender.SelectedValue = dt.Rows[i]["Col4"].ToString();
                DrpQualification.SelectedValue = dt.Rows[i]["Col5"].ToString();
                rowIndex++;
            }
        }
    }
    }

從代碼中, AddNewRow已經清除,而SetPreviouseData似乎用於將最后一個數據顯示到組件中(TextBox,RadioButtonList,DropDownList)。

    // Get the reference to the objects (TextBoxes, RadioButtonList, DropDownList)
    TextBox TextBoxName =  (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
    TextBox TextBoxAge = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtAge");
    TextBox TextBoxAddress = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtAddress");
    RadioButtonList RBLGender = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
    DropDownList DrpQualification = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("drpQualification");

    // Set the value of the objects to the last on the DataTable
    TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
    TextBoxAge.Text = dt.Rows[i]["Col2"].ToString();
    TextBoxAddress.Text = dt.Rows[i]["Col3"].ToString();
    RBLGender.SelectedValue = dt.Rows[i]["Col4"].ToString();
    DrpQualification.SelectedValue = dt.Rows[i]["Col5"].ToString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM