【问题标题】:How to hide a specific value(column) from the gridview?如何从网格视图中隐藏特定值(列)?
【发布时间】:2013-07-31 15:41:26
【问题描述】:

在我的 gridview 中,我有以下内容,如我在 page_load 中将 sql 与 gridview 绑定所示,因为我希望它在打开页面时加载。

SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
        conn.Open();

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect from memberreport", conn);
        da.Fill(ds);

        GWCase.DataSource = ds;
        GWCase.DataBind();

        conn.Close();

但是,我试图阻止财产、受害者和嫌疑人列出现在网格视图中。我用过

Visible = false;

在我的网格视图中,但它完全删除了我的网格视图(当然)。

我尝试在我的gridview中使用如下所示的boundfield并将可见性设置为false以专门将列可见性设置为false

    <asp:GridView ID="GWCase" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
        <RowStyle BackColor="White" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />

     <Columns>

       <asp:BoundField DataField="property" HeaderText="property" SortExpression="property" Visible="false"/>
       <asp:BoundField DataField="victim" HeaderText="victim" SortExpression="victim" Visible="false" />
       <asp:BoundField DataField="suspect" HeaderText="suspect" SortExpression="suspect" Visible="false" />

     </Columns>
    </asp:GridView>

但是,该列仍在显示中。如何从 gridview 中删除该 3 列。请不要让我从我的 sql 语句中删除 3 属性,因为我需要数据来实现更多功能。

我也尝试过我在这个thread in SO 中找到的这种方法

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[7].Visible = false;
        e.Row.Cells[8].Visible = false;
        e.Row.Cells[9].Visible = false;
    }

但效果不佳:/

问候。

【问题讨论】:

    标签: c# asp.net gridview boundfield


    【解决方案1】:

    您需要将此代码添加到行创建事件中。

        protected void yourGrid_RowCreated(object sender, GridViewRowEventArgs e)
        {
        e.Row.Cells[7].Visible = false;
        e.Row.Cells[8].Visible = false;
        e.Row.Cells[9].Visible = false;
        }
    

    编辑:

    另一个选项可以是在将数据源分配给网格视图后,您可以编写这些行 在您的代码中的这一行之后

       GWCase.DataSource = ds;
       GWCase.DataBind();
    
       GWCase.Columns[7].Visible = false;
       GWCase.Columns[8].Visible = false;
       GWCase.Columns[9].Visible = false;
    

    【讨论】:

    • 我将数据源和绑定放在了 page_load 中。如果我将其粘贴到 page_load 中,我将收到此错误 Error 16 'System.EventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
    • 添加行编辑并将其删除后,我的整个网格视图神奇地从我的页面中消失了。
    【解决方案2】:

    将gridview的AutoGenerateColumns属性设置为false(默认为true)。然后添加您想要在&lt;Columns&gt; 标记中显示的所有行,就像您对不想显示的列所做的那样。只要您自动生成列,Columns 标签就没有任何作用。

    【讨论】:

    • 但是,请问我的列和边界域语法是否正确?据我了解,Datafield 是我的数据库名称,headertext 将是它出现在 gridview 标题上的内容,但 sortexpression 呢?我用谷歌搜索,它说排序数据,我真的不需要它。我可以删除它吗?
    • 是的,你的语法在我看来是正确的。您对 DataField 和 HeaderText 的理解也是正确的,只要您不希望用户对 GridView 的特定列进行排序,就可以删除 SortExpression。作为 HeaderText,它是当用户单击列标题文本时要排序的列的数据库名称。
    • 嗯我已经做到了,我也删除了可见性。但是,当我选择行但标签不显示任何内容时,我使用此语法lblproperty.Text = GWCase.SelectedRow.Cells[8].Text; lblvictim.Text = GWCase.SelectedRow.Cells[9].Text; lblsuspect.Text = GWCase.SelectedRow.Cells[10].Text; 将第 3 列的值显示到标签中。
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多