无法从C#动态创建SQL表

时间:2011-08-27 09:47:05

标签: c# asp.net

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

我的ASPX代码是:

 <tr>
                <td colspan="2">
                   <asp:Button ID="btn_add_column" runat="server" Text="Click Here to Add column" 
                        onclick="btn_add_column_Click" />
<%--                         <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
--%>                </td>
            </tr>

 <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Column Name">
            <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="Data Type">
            <ItemTemplate>
                  <asp:DropDownList ID="ddldatatype" runat="server">
                  <asp:ListItem>varchar</asp:ListItem>
                  <asp:ListItem>int</asp:ListItem>
                  <asp:ListItem>numeric</asp:ListItem>
                  <asp:ListItem>uniqueidentifier</asp:ListItem>
                  <asp:ListItem>char</asp:ListItem>
                  </asp:DropDownList>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
             <asp:Button ID="ButtonDel" runat="server" Text="Delete Row" OnClick="ButtonDel_Click"/>
             <input type="hidden" runat="server" value="0" id="hiddencount" />
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

现在我要删除删除行的文本框

我在ButtonDel_Click上尝试了这个

 private void DeleteNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 0; i <= dtCurrentTable.Rows.Count-1; 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");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddldatatype");

                    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();
    }
    protected void ButtonDel_Click(object sender, EventArgs e)
    {
        DeleteNewRowToGrid();
    }

但是我收到了这个错误:

  

指数超出范围。必须是非负的且小于   集合的大小。
  参数名称:index

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

错误说明了一切,索引超出范围...... 可能在你的for循环中某处你调用了一个不存在的索引。

此外,你可以优化你的for循环:

for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
...
rowIndex--;

我认为你的rowIndex是导致错误的原因。首先你把它分配给0然后你尝试rowindex = 0 - 1.哪个变成-1 如果使用-1作为索引值,则会折叠。

编辑:
这意味着您应该将rowIndex --;更改为rowIndex ++;或将int rowIndex = 0;更改为其他更高的数字。

第二和第三编辑:

您可以这样做:

        for (int i = 0; i < (DataTable)ViewState["CurrentTable"].Rows.Count; i++)
        {
            //extract the TextBox values
            TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
            //TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
            DropDownList box2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("ddldatatype");

            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["RowNumber"] = i;
            drCurrentRow["Column1"] = box1.Text;
            drCurrentRow["Column2"] = box2.Text;
            //drCurrentRow["Column3"] = box3.Text;
            //add new row to DataTable
            dtCurrentTable.Rows.Add(drCurrentRow);
        }

我希望它能帮助你更进一步。

答案 1 :(得分:1)

您的代码已

rowIndex--;

而不是

rowIndex++;