在Databound Gridview中使单个字段可编辑

时间:2014-04-17 15:42:25

标签: c# asp.net gridview

我希望MAC列可以编辑,因为这是一个注册应用程序,这将允许用户更新他们的注册。我已经从SQL数据库绑定数据,并且工作正常。我只是不确定如何让当前的MAC绑定并仍然可以进行编辑。任何帮助将不胜感激,我已经发布了我的asp.net(目标框架4.5)代码。

<asp:GridView ID="DataDisplay" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" Height="78px" Width="727px" ViewStateMode="Enabled"
        OnRowCancelingEdit="DataDisplay_RowCancelingEdit" OnRowDeleting="DataDisplay_RowDeleting"
        OnRowEditing="DataDisplay_RowEditing" style="margin-right: 0px">
        <Columns>
            <asp:BoundField HeaderText="Device" DataField="Device" ReadOnly="true" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField HeaderText="MAC Address" DataField="MAC Address" ItemStyle-HorizontalAlign="Center" />
            <asp:BoundField HeaderText="Area" DataField="Area" ReadOnly="true" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField HeaderText="Date Registered" DataField="Date Registered" ReadOnly="true" ItemStyle-HorizontalAlign="Center"/>
            <asp:CommandField ShowEditButton="true" />
            <asp:CommandField ShowDeleteButton="true" />
        </Columns>
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <SortedAscendingCellStyle BackColor="#F4F4FD" />
        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
        <SortedDescendingCellStyle BackColor="#D8D8F0" />
        <SortedDescendingHeaderStyle BackColor="#3E3277" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:1)

替换&#34; MAC地址&#34;带有模板字段的BoundField:

<asp:TemplateField HeaderText="MAC Address" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblMacAddress" runat="server" Text='<%#Eval("MAC Address") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtMacAddress" Text='<%#Bind("[Mac Address]") %>' runat="server"></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>

修改

其他字段应仅包含ItemTemplate,如下所示:

<asp:TemplateField HeaderText="Device" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblDevice" runat="server" Text='<%#Eval("Device") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

BTW - 数据字段名称中的空格不是一个好主意!