Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在dataTable中添加文本框_Asp.net_Datatable - Fatal编程技术网

Asp.net 在dataTable中添加文本框

Asp.net 在dataTable中添加文本框,asp.net,datatable,Asp.net,Datatable,我想在我的dataTable中添加textBox。我的dataTable是动态创建的,并绑定到gridView。当我尝试在按钮上添加新行时,单击会出现以下错误: 索引超出范围。必须为非负数且小于 收藏 参数名称:索引 TextBox TextBox1 = (TextBox)GridView2.Rows[0].Cells[2].FindControl("TextBox1"); 这是我的密码: 标记: <asp:GridView ID="GridView2" runat="server"

我想在我的
dataTable
中添加
textBox
。我的
dataTable
是动态创建的,并绑定到
gridView
。当我尝试在按钮上添加新行时,单击会出现以下错误:

索引超出范围。必须为非负数且小于 收藏

参数名称:索引

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();
        }
    }
}
private void addnewrecordrowtorridf()
{
//创建数据表
DataTable dt=新的DataTable();
数据行dr;
dt.TableName=“Markici”;
//为DataTable创建列
添加(新数据列(“FirmaID”,typeof(System.Int32));
添加(新的数据列(“Godina”,typeof(System.Int32));
添加(新的数据列(“KasaID”,typeof(System.Int32));
Add(新的数据列(“MarkicaID”,typeof(System.Int64));
添加(新的数据列(“数据”),类型(System.DateTime));
添加(新数据列(“Masa”,typeof(System.Int32));
添加(新的数据列(“VrabotenID”,typeof(System.Int32));
Add(新的数据列(“Artikal”,typeof(System.String));
Add(新的数据列(“Cena1”,typeof(System.String));
添加(新数据列(“Kolicina”,typeof(System.Decimal));
添加(新数据列(“Smena”,typeof(System.Int32));
添加(新数据列(“VkIznos”,typeof(System.Decimal));
Add(新数据列(“VkDanok”,typeof(System.Decimal));
Add(新数据列(“SysDatum”,typeof(System.DateTime)));
Add(新数据列(“Vid”,typeof(System.String));
Add(新数据列(“EdMera”,typeof(System.String));
Add(新的数据列(“ArtikalID”,typeof(System.String));
Add(新的数据列(“TarifaID”,typeof(System.Int32));
Add(新数据列(“Zabeleshka”,typeof(System.String));
dr=dt.NewRow();
dt.Rows.Add(dr);
视图状态[“Markici2”]=dt;
如果(ViewState[“Markici2”]!=null)
{
DataTable dtCurrentTable=(DataTable)视图状态[“Markici2”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{

对于(int i=1;我在查找控件时不使用单元格。框架可以这样做。
GridView2.Rows[0]。FindControl(“TextBox1”);
并且不要问同一个问题两次。编译错误描述:在编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码。编译器错误消息:CS0103:当前上下文中不存在名称“TextBox1”源错误:索引超出范围。必须为非负且小于集合的大小。参数名称:索引TextBox TextBox1=(TextBox)GridView40。行[0]。FindControl(“TextBox1”);