0

我有一个绑定到数据源的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>

在此处输入图像描述

如您所见,自动生成的列设置为 true,并且必须保持这种状态。其中一列是 SQL 位值,因此它表示为复选框。我希望能够只编辑复选框列,而不使用“AutoGenerateEditButton”属性。我只想:

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

1 回答 1

2

无论如何,自动生成的列几乎无法直接操作,因此没有简单的方法可以做到这一点。所以你可以做的是创建一个自定义列,它总是在任何自动生成的列之前出现(同样,这种行为不能改变),并隐藏自动生成的位列。

此处描述了如何隐藏该列。本质上你不能使用 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>

诚然,这是相当骇人听闻的,但这会让你几乎到达你想要的地方 - 显示和可编辑的 bool 列。

于 2017-03-29T15:45:50.313 回答