如何隐藏GridView的一个特定的值

如何隐藏GridView的一个特定的值

本文介绍了如何隐藏GridView的一个特定的值(列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GridView我有这样的事情如下所示,我与在Page_Load gridview的SQL的结合,我希望它在打开的页面加载。

In my gridview i have this following things as shown in my binding of sql with gridview in the page_load as i want it to load upon opening the page.

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();

不过,我出现在GridView试图prevent财产,受害者和犯罪嫌疑人列。我用

However, i'm trying to prevent property, victim and suspect column from appearing in the gridview. I used

Visible = false;

在我的GridView控件,但它完全删除我的GridView(当然)。

in my gridview but it totally remove my gridview( of course ).

我试着用绑定列在GridView的我如下图所示,并设置能见度为假,专门设置一个列的可见假

I tried using boundfield as shown below in my gridview and set the visibility as false to specifically set a column visiblity as 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属性,因为我需要用于其它功能的数据。

However, the column are still being displayed. How do i remove that 3 column from the gridview. Please do not ask me to remove the 3 attribute from my sql statement as i need the data for further functions.

我也尝试过这种方法,我在这个发现SO

I have also tried this method i found in this 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;
    }

但它没有工作,以及:/

But it didnt work as well :/

问候。

推荐答案

您需要行创建的事件添加此code。

You need to add this code in row created event.

    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;
    }

编辑:

另一个选项可以是到网格视图分配数据源后,你写这几行
这条线在code后

Another option can be that after assigning datasource to grid view you write these linesafter this line in your code

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

   GWCase.Columns[7].Visible = false;
   GWCase.Columns[8].Visible = false;
   GWCase.Columns[9].Visible = false;

这篇关于如何隐藏GridView的一个特定的值(列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:10