Gridview,自动生成列,复选框列可编辑

时间:2017-03-29 15:21:31

标签: asp.net gridview checkbox edit autogeneratecolumn

我有一个绑定到数据源的gridview。

 <asp:GridView ID="DocumentReviewGrid" runat="server" AllowPaging="True" AllowSorting="True"
                    EnableModelValidation="True" Width="100%" BorderStyle="None" 
                    CssClass="docnav_data" BorderWidth="1px" GridLines="None" DataSourceID="DocumentReviewDataSource"
                    HorizontalAlign="Left" OnRowDataBound="DocumentReviewGrid_RowDataBound" 
                    OnRowCreated="DocumentReviewGrid_RowCreated" CellSpacing="5"
                    PageSize="20" OnPageIndexChanged="DocumentReviewGrid_PageIndexChanged">
                    <AlternatingRowStyle BackColor="#EBF2F9" BorderStyle="None" />
                    <FooterStyle HorizontalAlign="Left" />
                    <HeaderStyle BackColor="#E7E7E7" HorizontalAlign="Left" />
                    <PagerSettings Mode="NumericFirstLast" Position="Top" PageButtonCount="4" />
                    <PagerStyle HorizontalAlign="Center" />                       
                </asp:GridView>

enter image description here

正如您所看到的,Autogenerated Column设置为true,并且必须保持这样。其中一列是SQL位值,因此它表示为复选框。我希望能够仅编辑复选框列,而不使用&#34; AutoGenerateEditButton&#34;属性。我只想:

  • 能够选中/取消选中复选框(我被困在这里)
  • 使用外部按钮执行单次更新
  • 其他列必须是只读

1 个答案:

答案 0 :(得分:2)

无论如何都无法直接操作自动生成的列,因此没有简单的方法可以实现。所以你可以做的是创建一个自定义列,它总是在任何自动生成的列之前出现(再次,这种行为无法更改),并隐藏自动生成的位列。

here描述了如何隐藏列。基本上你不能使用Columns集合,所以需要这样做:

protected void DocumentReviewGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[X].Visible = false; // hides the first column
}

此处X是要隐藏的列的从0开始的索引。

现在要预先添加列,只需按照您想要的方式定义,离开AutoGenerateColumns="true"

<asp:GridView ID="DocumentReviewGrid"...>
    <Columns>
        <asp:CheckBoxField HeaderText="Esclusione" DataField="Esclusione" />
    </Columns>
</asp:GridView>

不可否认,这是非常hackish,但这将使你几乎到达你想要的地方 - bool列显示和编辑。

相关问题