【问题标题】:Gridview, Autogenerated Column, Checkbox Column EditableGridview、自动生成的列、复选框列可编辑
【发布时间】:2017-08-23 04:30:52
【问题描述】:

我有一个绑定到数据源的 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”属性。我只想:

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

【问题讨论】:

  • 我想不出一个干净的方法,但知道一个 hacky 解决方法来做到这一点。但首先要问两个问题:您能否依赖列的顺序始终相同,如果此可编辑列首先出现在网格中是否有问题?
  • 我可以依赖列的顺序,但可编辑列宁愿位于列的中间。如果您有一个需要将列放在首位的解决方案,请告诉我。

标签: asp.net gridview checkbox edit autogeneratecolumn


【解决方案1】:

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

描述了如何隐藏列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>

诚然,这很老套,但这会让您几乎到达您想要的地方 - 显示和编辑布尔列。

【讨论】:

  • 我试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多