简体   繁体   English

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

[英]Gridview, Autogenerated Column, Checkbox Column Editable

I have a gridview binded to a datasource. 我有一个绑定到数据源的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>

在此处输入图片说明

As you can see Autogenerated Column is set to true, and it must be kept like that. 如您所见,“自动生成的列”设置为true,因此必须将其保留为此类。 One of the column is a SQL bit value, so it's represented as checkbox. 列之一是SQL位值,因此将其表示为复选框。 I would like to be able to edit the checkbox column only, without using "AutoGenerateEditButton" property. 我只想编辑复选框列,而不使用“ AutoGenerateEditButton”属性。 I would just like to: 我只想:

  • be able to check/uncheck the checkbox (I am stuck here) 能够选中/取消选中复选框(我被困在这里)
  • performing a single update using an external button 使用外部按钮执行单个更新
  • the other columns must be readonly 其他列必须是只读的

Autogenerated columns cannot be manipulated directly in pretty much anyway, so there is no simple way to do it. 无论如何,几乎不能直接对自动生成的列进行直接操作,因此没有简单的方法可以做到这一点。 So what you could do is create a custom column, which will always come first before any auto generated columns (again, this behavior cannot be changed), and hide the auto generated bit column. 因此,您可以做的是创建一个自定义列,该列将始终排在任何自动生成的列之前(同样,此行为无法更改),然后隐藏自动生成的位列。

How to hide the column is described here . 这里介绍如何隐藏列。 Essentially you cannot use Columns collection, so need to do this: 本质上,您不能使用Columns集合,因此需要执行以下操作:

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

Here X is the 0-based index of the column to hide. X是要隐藏的列的从0开始的索引。

And now to prepend the column just define it the way you want, leaving AutoGenerateColumns="true" : 现在,以该列为开头,只需按所需方式定义它即可,保留AutoGenerateColumns="true"

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

Admittedly this is quite hackish, but that will get you almost where you want - bool column displayed and editable. 诚然,这确实有点hacker,但这将使您几乎可以找到所需的位置-显示和可编辑的bool列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM