locked
How can I hide/ignore the first row from a datasource in a gridview? RRS feed

  • Question

  • User-938890548 posted

    We are using a gridview which pulls in data from a sql table.

    Is it possible to hide the latest/newest row in the grid view?

    Here's what we are trying to do:
    Currently, the gridview works as a notice board and we eventually want to show the latest article in a bigger format, so we will probably need to split this into 2 separate data sources: 1. which sows the top/latest article and the other datasource to pull in the remaining data (from the 2nd piece of data onwards). Hence the need to hide the first piece/row of data.

    Here is a snippet of some of the Grid View:

      <asp:GridView ID="gvNews" runat="server" AutoGenerateColumns="False" 
                OnRowCommand="gvNews_RowCommand" DataSourceID="DSnews" >
                
                 <Columns>
                        <asp:BoundField DataField="NewsDate" htmlEncode="False" 
                            DataFormatString="{0:dd/MM/yyyy}" >  
                        </asp:BoundField>
    
                        <asp:TemplateField> 
                            <ItemTemplate>
                               <strong><%#Eval("NewsName")%></strong>
                               <br />
                               <%#Eval("NewsSummary")%>
                            </ItemTemplate>  
                        </asp:TemplateField> 
                     <asp:BoundField DataField="NewsAction">
                     </asp:BoundField>
                    </Columns>
    
                   <PagerSettings Mode="NumericFirstLast" PageButtonCount="3" />
                <SelectedRowStyle CssClass="SelectedRowStyle" />
            </asp:GridView> 
    

    Would hiding the first row of data be done in the gridview or on the datasource code?

    thanks.

    Wednesday, May 8, 2013 6:34 AM

Answers

  • User220959680 posted

    Incorrect information provided in earlier post. Sorry!

    //Get the first row based on the Row Index
    GridViewRow row = gvNews.Rows[0];
    row.Visible = false;           
                

    Note that above is successfully tested in Page_Load event where the GridView data is binded to display on page.

    Test this in Page_Load event insted of GridView_DataBound event and post your results here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2013 10:42 AM

All replies

  • Wednesday, May 8, 2013 6:36 AM
  • User-938890548 posted

    I modified the example code (which, I believe hides a row based on the user ).

    I just want to hide a row for every user, so I removed the IF and CASE statement and tried this:

    	Private Sub gvNews_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvNews.RowDataBound
    	 Dim Usr As String
                Usr = e.Row.Cells(1).Text
                e.Row.Visible =False
    End Sub
    


    but that didnt work.

    Wednesday, May 8, 2013 7:04 AM
  • User-938890548 posted

    I've tried this:

    Private Sub gvNews_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvNews.RowDataBound
    	if e.Row.RowType = DataControlRowType.DataRow then
    		e.Row.Cells(2).Visible = False
    	end if
    End Sub
    

    but this hides an entire column of data - I just want to hide the first row.

    I think I'm making this more complicated than it is?

    I wonder if it's possible in gridview to hide the first row?

    Wednesday, May 8, 2013 9:11 AM
  • User220959680 posted

    e.Row.Cells(2).Visible = False

    Hide the first row not the column.

    e.Row(0).visible = False;

    similar to the above....

    Wednesday, May 8, 2013 9:27 AM
  • User-938890548 posted

    e.Row(0).visible = False;

    sorry, this crashes the page.

    Wednesday, May 8, 2013 9:41 AM
  • User220959680 posted
    GridView.Rows[Row Index here].visible = false;

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rows.aspx

    sorry for the above incorrect response due to copy/paste error.

    Wednesday, May 8, 2013 8:01 PM
  • User-938890548 posted

    thanks for the reply,

    i treid this:

    gvNews.Rows[1].visible = false;
    and
    gvNews.Rows(0).visible = false;

     

    it gave this error:

    Compiler Error Message: BC30545: Property access must assign to the property or use its value.
    

    Thursday, May 9, 2013 5:52 AM
  • User220959680 posted

    Incorrect information provided in earlier post. Sorry!

    //Get the first row based on the Row Index
    GridViewRow row = gvNews.Rows[0];
    row.Visible = false;           
                

    Note that above is successfully tested in Page_Load event where the GridView data is binded to display on page.

    Test this in Page_Load event insted of GridView_DataBound event and post your results here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2013 10:42 AM
  • User-938890548 posted

    thanks.

    that works!

    (just had to convert to vb)

    Dim row As GridViewRow = gvNews.Rows(0)
    row.Visible = False

    Friday, May 10, 2013 4:53 AM