2013-07-31 74 views
1

在我的gridview中,我有以下的东西,如我在页面加载中绑定sql和gridview所示,因为我希望它在打开页面时加载。如何从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(); 

但是,我试图防止财产,受害者和嫌疑人列出现在gridview。我用

Visible = false; 

在我的gridview,但它完全删除我的gridview(当然)。

我试图使用的BoundField如在我的GridView如下所示,并设置可见性为假以专门设置的列的可见假然而,仍然被显示

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

但它没有工作,以及:/

问候。

回答

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; 
+0

还是一样 –

+0

@TeoChuenWeiBryan查看我的更新回答 – Ehsan

+0

我将我的数据源和绑定放在我的page_load中。如果我将它粘贴到page_load中,我会得到这个错误'错误'System.EventArgs'不包含'Row'的定义,也没有接受类型'System'的第一个参数的扩展方法'Row'。可以找到EventArgs'(你是否缺少使用指令或程序集引用?)' –

0

设置你的GridView的财产AutoGenerateColumns写这几行 此行之后错误(默认情况下为true)。然后添加您想要在<Columns>标签内显示的所有行,就像您对不想显示的列所做的那样。只要您自动生成列,Columns标记就不起作用。

+0

但是,我可以问我的列和boundfield语法是否正确?从我的理解数据字段是我的数据库名称,headertext将出现在gridview标题上,但排序表达式如何。我GOOGLE了,它说排序数据,我真的不需要它。我可以删除它吗? –

+0

是的,你的语法对我来说是正确的。您对DataField和HeaderText的理解也是正确的,只要您不希望用户对GridView的特定列进行排序,就可以删除SortExpression。作为HeaderText,它是在用户单击列标题文本时要排序的列的数据库名称。 – Rob

+0

嗯,我已经做到了,我也删除了可见性。不过,我用这个语法'lblproperty。Text = GWCase.SelectedRow.Cells [8] .Text; lblvictim.Text = GWCase.SelectedRow.Cells [9] .Text; lblsuspect.Text = GWCase.SelectedRow.Cells [10] .Text;'当我选择行但标签不显示任何内容时,将3列的值显示在标签中。 –