在gridview中编辑相同的单元格

时间:2013-05-10 15:54:39

标签: c# asp.net linq-to-sql gridview

你好我正在创建一个购物应用程序,我有一个购物车,我有一些列从后面的代码中添加,有些列是模板字段,这些字段在数据库中不存在但希望将来使用。 / p>

这是我的购物车

代码:

<asp:GridView ID="GridViewProduct" runat="server" CellPadding="5" CssClass="grid"
    ForeColor="#333333" OnRowCreated="GridViewProduct_RowCreated" OnRowDataBound="GridViewProduct_RowDataBound"
    ShowFooter="True" CellSpacing="1" 
    onrowupdating="GridViewProduct_RowUpdating">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:ButtonField Text="Remove From Cart" CommandName="RemoveFromCart" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="TextBoxQuantity" runat="server" Width="50px">1</asp:TextBox><br />
                <asp:LinkButton ID="LinkButtonEditQuantity" runat="server"  CommandName="EditTextBox">edit</asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButtonUpdate" runat="server">Update</asp:LinkButton>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total Price">
            <ItemTemplate>
                <asp:Label ID="LabelTotalPrice" runat="server" Text="0"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F8FAFA" />
    <SortedAscendingHeaderStyle BackColor="#246B61" />
    <SortedDescendingCellStyle BackColor="#D4DFE1" />
    <SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>

enter image description here

我在模板字段中有一个编辑链接按钮,在editItem模板和上面的文本框中有更新链接按钮,以便在用户单击编辑链接按钮时编辑文本框。

我的问题是:如何编辑同一列中的文本框而不是整行。

我应该使用哪种方法来实现编辑相同的单元格rowupdating,rowcommand并保存会话中的值,并将编辑后的值乘以数量和单价以显示在gridview的总价格行中。

请帮助我实施正确的方法来实现这一点,因为我坚持了很多天..请

1 个答案:

答案 0 :(得分:1)

在gridview模板上,查看EditRowTemplate,您将看到它包含所有可编辑的控件(很可能是文本框),删除文本框并添加标签,并将它们绑定到您的数据源。这样,数据仍然显示但不可编辑(除了您想要编辑的控件)

尝试一下,如果您需要更多帮助,请告诉我。