问题
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();
However, i'm trying to prevent property, victim and suspect column from appearing in the gridview. I used
Visible = false;
in my gridview but it totally remove my gridview( of course ).
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>
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.
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 :/
Regards.
回答1:
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;
}
Edit:
Another option can be that after assigning datasource to grid view you write these lines after 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;
回答2:
Set the property AutoGenerateColumns
of your gridview to false (it is true by default). Then add all rows you want to show inside the <Columns>
tags as you already did with the columns you do not want to show. The Columns tag has no effect as long as you auto-generate columns.
来源:https://stackoverflow.com/questions/17964878/how-to-hide-a-specific-valuecolumn-from-the-gridview