2013-08-28 74 views
0

我有下面的代碼,我在本教程中找到:值不在文本框中添加

http://geekswithblogs.net/dotNETvinz/archive/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx

Default.aspx的

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" 
          AutoGenerateColumns="false"> 
     <Columns> 
     <asp:BoundField DataField="RowNumber" HeaderText="Row Number" /> 
     <asp:TemplateField HeaderText="Header 1"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Header 2"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Header 3"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
      </ItemTemplate> 
      <FooterStyle HorizontalAlign="Right" /> 
      <FooterTemplate> 
       <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" onclick="ButtonAdd_Click" /> 
      </FooterTemplate> 
     </asp:TemplateField> 
     </Columns> 
</asp:gridview> 

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e){ 
     if (!Page.IsPostBack){ 
      SetInitialRow(); 
     } 
} 


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(){ 

     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 box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3"); 

        drCurrentRow = dtCurrentTable.NewRow(); 
        drCurrentRow["RowNumber"] = i + 1; 
        drCurrentRow["Column1"] = box1.Text; 
        drCurrentRow["Column2"] = box2.Text; 
        drCurrentRow["Column3"] = box3.Text; 

        rowIndex++; 
       } 

       //add new row to DataTable 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       //Store the current data to ViewState 
       ViewState["CurrentTable"] = dtCurrentTable; 

       //Rebind the Grid with the current data 
       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 = 1; i < dt.Rows.Count; i++) 
       { 
        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3"); 


        box1.Text = dt.Rows[i]["Column1"].ToString(); 
        box2.Text = dt.Rows[i]["Column2"].ToString(); 
        box3.Text = dt.Rows[i]["Column3"].ToString(); 

        rowIndex++; 

       } 
      } 
     } 
} 

protected void ButtonAdd_Click(object sender, EventArgs e){ 
     AddNewRowToGrid(); 
} 

現在的問題是,讓我們說,當初始網格出現,我添加一些數據,然後clic k添加行。我在那一行添加的數據保持不變。但現在,讓我說我增加了5行,然後我去了第3行,我添加了一些數據,然後點擊添加行,數據消失,從不出現。只有當我在最後一行添加數據然後點擊添加行時,數據纔會顯示。有誰知道爲什麼會發生這種情況?

+0

「ButtonAdd_Click」在哪裏?如何調用AddNewRowToGrid? –

+0

其在頁腳模板中的按鈕的onclick事件 –

+0

@ P.Brian.Mackey - 位於發佈代碼的最底部。 –

回答

0

它發生,因爲這個循環,:

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
{ 
    //extract the TextBox values 
    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3"); 

    drCurrentRow = dtCurrentTable.NewRow(); 
    drCurrentRow["RowNumber"] = i + 1; 
    drCurrentRow["Column1"] = box1.Text; 
    drCurrentRow["Column2"] = box2.Text; 
    drCurrentRow["Column3"] = box3.Text; 

    rowIndex++; 
} 

這是要通過在數據表中的所有行和建drCurrentRow對象,但它僅添加drCurrentRow對象循環之外:

//add new row to DataTable 
dtCurrentTable.Rows.Add(drCurrentRow); 

因此drCurrentRow的內容是最後一行。

UPDATE:

爲了解決這個問題,我建議你把它所以只有一個行可以添加一個時間,然後,而不是通過所有的數據表中的行的循環,只是得到從數據最後一行並將其用作「新」行添加到數據表的數據,如下所示:

//extract the TextBox values 
TextBox box1 = (TextBox)Gridview1.Rows[dtCurrentTable.Rows.Count - 1].Cells[1].FindControl("TextBox1"); 
TextBox box2 = (TextBox)Gridview1.Rows[dtCurrentTable.Rows.Count - 1].Cells[2].FindControl("TextBox2"); 
TextBox box3 = (TextBox)Gridview1.Rows[dtCurrentTable.Rows.Count - 1].Cells[3].FindControl("TextBox3"); 

drCurrentRow = dtCurrentTable.NewRow(); 
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1; 
drCurrentRow["Column1"] = box1.Text; 
drCurrentRow["Column2"] = box2.Text; 
drCurrentRow["Column3"] = box3.Text; 

//add new row to DataTable 
dtCurrentTable.Rows.Add(drCurrentRow); 
+0

我該如何解決這個問題? –

+0

@BradHazelnut - 我會建議這樣做,所以一次只能將一行添加到網格中。而不是循環遍歷所有行,只需使用數據表中最後一項的數據來填充「新」行。在我的回答中看到'UPDATE:'。 –

+0

我剛剛嘗試過,它似乎給出了相同的結果 –