1

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.

Community
  • 1
  • 1
Bryan
  • 8,488
  • 14
  • 52
  • 78

2 Answers2

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;
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • I placed my datasource and binding in my page_load. And if i were to paste that into the page_load i will get this error `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?)` – Bryan Jul 31 '13 at 09:06
  • After adding the rowediting and removing it, my entire gridview disappears magically from my page. – Bryan Jul 31 '13 at 09:35
0

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.

Rob
  • 919
  • 7
  • 16
  • However, may i ask if my column and boundfield syntax are correct? From my understanding Datafield is my database name, headertext will be what it appears on gridview header but what about sortexpression. I googled and it says sorting data which i dont really need it. can i just remove it? – Bryan Jul 31 '13 at 08:18
  • Yes, your syntax looks correct to me. Your understanding of DataField and HeaderText is correct as well and you can remove the SortExpression as long as you do not want the user to sort specific columns of your GridView. As HeaderText it is the database name of the column you want to sort when the user clicks on the column header text. – Rob Jul 31 '13 at 08:29
  • hmm i have done it and i also remove the visibility as well. However, i used this syntax `lblproperty.Text = GWCase.SelectedRow.Cells[8].Text; lblvictim.Text = GWCase.SelectedRow.Cells[9].Text; lblsuspect.Text = GWCase.SelectedRow.Cells[10].Text;` to display out the value of the 3 column into the label when i select the row but the label does not show anything. – Bryan Jul 31 '13 at 08:31