我想在我的textBox
中添加dataTable
。我的dataTable
是动态创建的并绑定到gridView
。当我尝试在按钮上添加新行时,点击给我这个错误:
指数超出范围。必须是非负数且小于 集合。
参数名称:index
TextBox TextBox1 = (TextBox)GridView2.Rows[0].Cells[2].FindControl("TextBox1");
这是我的代码:
标记:
<asp:GridView ID="GridView2" runat="server"
ShowHeader="true"
OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnTest" runat="server"
CssClass="button2"
OnClick="btnTest_Click"
Text="-"
Width="100px"
Font-Bold="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
private void AddNewRecordRowToGridf()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "Markici";
//creating columns for DataTable
dt.Columns.Add(new DataColumn("FirmaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Godina", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("KasaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("MarkicaID", typeof(System.Int64)));
dt.Columns.Add(new DataColumn("Datum", typeof(System.DateTime)));
dt.Columns.Add(new DataColumn("Masa", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("VrabotenID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Artikal", typeof(System.String)));
dt.Columns.Add(new DataColumn("Cena1", typeof(System.String)));
dt.Columns.Add(new DataColumn("Kolicina", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("Smena", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("VkIznos", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("VkDanok", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("SysDatum", typeof(System.DateTime)));
dt.Columns.Add(new DataColumn("Vid", typeof(System.String)));
dt.Columns.Add(new DataColumn("EdMera", typeof(System.String)));
dt.Columns.Add(new DataColumn("ArtikalID", typeof(System.String)));
dt.Columns.Add(new DataColumn("TarifaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Zabeleshka", typeof(System.String)));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["Markici2"] = dt;
if (ViewState["Markici2"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici2"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox TextBox1 = (TextBox)GridView2.Rows[0].Cells[2].FindControl("TextBox1");
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label12.Text;
drCurrentRow["Cena1"] = Label13.Text;
if (Session["MinusVrganj"] != null)
{
drCurrentRow["Kolicina"] = Convert.ToInt32(Session["MinusVrganj"]) + Counter; //Label5.Text; //count;
}
drCurrentRow["Kolicina"] = Label11.Text; //count;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label17.Text;//Counter1 * Convert.ToDecimal(Label13.Text); //Label17.Text;
drCurrentRow["VkDanok"] = Label18.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label24.Text;
drCurrentRow["Edmera"] = Label16.Text;
drCurrentRow["ArtikalID"] = Label34.Text;
drCurrentRow["TarifaID"] = Label14.Text;
drCurrentRow["Zabeleshka"] = TextBox1.Text;
}
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}