我想補充的textBox到dataTable的row.I不知道該怎麼辦that.Is有可能的textBox添加到DataTable首先,它給我這個錯誤:添加文本框
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
這裏是我的代碼:
標記:
<asp:GridView ID="GridView2" runat="server" ShowHeader="false" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<ItemTemplate >
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代碼背後:
private void AddNewRecordRowToGrid()
{
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "table";
dt.Columns.Add(new DataColumn("Zabeleshka", typeof(TextBox)));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["marks"] = dt;
if (ViewState["marks"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["marks"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox TextBox1 = (TextBox)GridView2.Rows[0].FindControl("TextBox1");
drCurrentRow["Zabeleshka"] = TextBox1.Text;
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["marks"] = dtCurrentTable;
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}
}
'Textbox'是控制和'Datatable'是在內存中的表持有數據,而不是控制,所以你需要存儲在數據表中的值並綁定將具有相關文本框的gridview。 – 2015-03-03 09:32:24
@RahulSingh現在我該怎麼辦? - 我該如何解決這個問題? – buba 2015-03-03 09:33:35
@RahulSingh例子? – buba 2015-03-03 09:45:53