0

我有一个gridview,我试图循环以获取要更新的值并将其存储在我的数据库中。网格视图代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" 
                Width="1020px" EnableTheming="True" PageSize="25" CellPadding="4" 
                ForeColor="#333333" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="Column1" HeaderText="Col1" ReadOnly="True" >
                    <ItemStyle Width="35px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Column2" HeaderText="Col2" />
                    <asp:BoundField DataField="Column3" HeaderText="Col3" />
                    <asp:BoundField DataField="Column4" HeaderText="Col4" />
                    <asp:BoundField DataField="Column5" HeaderText="Col5" />
                    <asp:TemplateField HeaderText="Col6">
                        <EditItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column6") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text="$"></asp:Label>&nbsp;
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column6", "{0:f}") %>' CssClass="boxright"></asp:TextBox>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="Column7" ReadOnly="True" >
                    <ItemStyle Width="35px" />
                    </asp:BoundField>
                    </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
                <PagerSettings Mode="NextPreviousFirstLast" />
                <PagerStyle BackColor="#003399" ForeColor="White" HorizontalAlign="Left" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>

我正在使用按钮单击事件来触发存储的 sql 过程来更新我的表,如下所示...

Protected Sub btnB_Save_Click(sender As Object, e As EventArgs) Handles btnB_Save.Click
Dim gvRow As GridViewRow
    Dim str As String = ""
    For Each gvRow As GridViewRow In gv_Budgets.Rows
        str = ((str + gvRow.Cells(0).Text) + (gvRow.Cells(1).Text) + (gvRow.Cells(2).Text) + (gvRow.Cells(3).Text) + (gvRow.Cells(4).Text) + (gvRow.Cells(6).Text))
    Dim dtRow As TextBox = CType(gvRow.FindControl("TextBox1"), TextBox)
    Response.Write(dtRow.Text)

    dt = dal.ExecuteStoredProc(DAL.dbType.SqlServer, "UpdateData", "@col1", str + gvRow.Cells(0).Text, "@col2", str + gvRow.Cells(1).Text, "@col3", str + gvRow.Cells(2).Text, "@col4", str + gvRow.Cells(3).Text, "@col5", str + gvRow.Cells(4).Text, "@col6", dtRow.Text, "@col7", str + gvRow.Cells(6).Text, "@txt1", Textbox2.Text, "@txt2", Textbox3.Text)

    Next
End Sub 

但是,我在“dt(datatable)”表达式下不断收到那些蓝色的语法错误行......关于如何或为什么这样做的任何想法?

4

1 回答 1

0

好吧,“变量 gvRow 隐藏...”错误正在发生,因为您已经在循环之外声明了 gvRow,然后在循环的定义中重新声明了它。

Dim gvRow As GridViewRow    ' First declaration
Dim str As String = ""
For Each gvRow As GridViewRow In gv_Budgets.Rows    ' Skip the "As GridViewRow here

另一个错误来自您的表达:

dt = dal.ExecuteStoredProc( ...

我假设(因为你没有列出它) dt 是某个地方的 DataTable 类型的一些全局变量。在这种情况下,您的 sProc 似乎正在返回一个字符串并尝试将其插入到数据表对象中。确保您的 sProc 返回有效的数据表。同时,您可能会尝试将结果插入到字符串中,以使事情正常进行,直到您可以解决为止。

于 2012-05-25T12:59:06.700 回答