使用gridview / templatefield更新SQL数据库

时间:2012-11-06 00:53:15

标签: vb.net gridview sqldatasource templatefield

任何人都知道如何获取Templatefield的文本框列,使用已经在gridview所在的gridview中使用的SqlDataSource和使用更新函数的按钮字段将数据输入的数据反馈回sql数据库?我只是想知道是否有人知道如何让这个工作,因为我还没有找到任何方法这样做。

如果你们中的任何人需要更多信息来帮助你解决这个难题,请告诉我。

如果我启用编辑并更改文本框中的信息以及同一行中另一列的字段,我能够获取反馈到SQL服务器的信息,但如果我只更改了它,它将不会更新文本框中的信息,无论是处于编辑模式还是仅单击更新按钮。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<center>
<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Brdcst_Nbr" DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." Width="657px">
        <Columns>
            <asp:BoundField DataField="Brdcst_Nbr" HeaderText="Brdcst_Nbr"     ReadOnly="True" 
                SortExpression="Brdcst_Nbr" />
            <asp:BoundField DataField="Item_Nbr" HeaderText="Item_Nbr" 
                SortExpression="Item_Nbr" ReadOnly="True" />
            <asp:CheckBoxField DataField="IsPicked" HeaderText="IsPicked" 
                SortExpression="IsPicked" />
            <asp:TemplateField SortExpression="Row" HeaderText="Row">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Mode="twoway" AutoPostBack="true" CausesValidation="true" Text='<%# Bind("Row") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Mode="twoway" AutoPostBack="true" CausesValidation="true" Text='<%# Bind("Row") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Update" Text="Update" />
            <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" />
        </Columns>
        <EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:ORDCConnectionString1 %>" 
        DeleteCommand="DELETE FROM [Test_New_Transpick] WHERE [Brdcst_Nbr] = @original_Brdcst_Nbr AND (([Item_Nbr] = @original_Item_Nbr) OR ([Item_Nbr] IS NULL AND @original_Item_Nbr IS NULL)) AND [IsPicked] = @original_IsPicked AND (([Row] = @original_Row) OR ([Row] IS NULL AND @original_Row IS NULL))" 
        InsertCommand="INSERT INTO [Test_New_Transpick] ([Brdcst_Nbr], [Item_Nbr], [IsPicked], [Row]) VALUES (@Brdcst_Nbr, @Item_Nbr, @IsPicked, @Row)" 
        UpdateCommand="UPDATE [Test_New_Transpick] SET [IsPicked] = @IsPicked, [Row] = @Row WHERE [Brdcst_Nbr] = @original_Brdcst_Nbr" 
        OldValuesParameterFormatString="original_{0}" 
        ProviderName="<%$ ConnectionStrings:ORDCConnectionString1.ProviderName %>" 
        SelectCommand="SELECT * FROM [Test_New_Transpick]" >
        <DeleteParameters>
            <asp:Parameter Name="original_Brdcst_Nbr" Type="String" />
            <asp:Parameter Name="original_Item_Nbr" Type="String" />
            <asp:Parameter Name="original_IsPicked" Type="Boolean" />
            <asp:Parameter Name="original_Row" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Brdcst_Nbr" Type="String" />
            <asp:Parameter Name="Item_Nbr" Type="String" />
            <asp:Parameter Name="IsPicked" Type="Boolean" />
            <asp:Parameter Name="Row" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Item_Nbr" Type="String" />
            <asp:Parameter Name="IsPicked" Type="Boolean" />
            <asp:Parameter Name="Row" Type="String" />
            <asp:Parameter Name="original_Brdcst_Nbr" Type="String" />
            <asp:Parameter Name="original_Item_Nbr" Type="String" />
            <asp:Parameter Name="original_IsPicked" Type="Boolean" />
            <asp:Parameter Name="original_Row" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

</div>
</form>
</center>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

因此,将SQLAatabase的属性从CompareAllValues更改为OverwriteChanges可修复此问题。它现在允许通过更改需要更新的行/列中的文本框而不必更改任何其他内容来将更改写回到sql数据库。