Hide Gridview when the data is not present

10,791

Solution 1

protected void GridView1_DataBound(object sender, EventArgs e)
{
    if (this.GridView1.Rows.Count == 0)
      this.GridView1.Visible = false; 
}

Solution 2

<asp:Gridview id="gridview" ...... 
      Visible='<%# ((ICollection)gridview.DataSource).Count == 0 ? false : true %>'>
Share:
10,791
John Smith
Author by

John Smith

Full Stack Developer =&gt; Angular 2+, Angularjs, C#, SQL, Jquery, Javascript

Updated on June 04, 2022

Comments

  • John Smith
    John Smith almost 2 years

    I have a Gridview that has filters that can fire off the emptydata template. However when this is displayed I can still see the outline of my gridview. How can I make the Gridview disappear when the data is not present?

  • John Smith
    John Smith over 14 years
    sounds great...but how can i do that in code and where? Gridview databound? how can i get access to the emptydata template in code?
  • jscharf
    jscharf over 14 years
    GridView is an ASP.NET control, which means it will have a 'Visible' property, allowing you to show or hide the control itself. GridView.Visible = false; will prevent the control from being rendered at all.
  • John Smith
    John Smith over 14 years
    What i wanted was for the Gridview borders to be hidden. Databoud was the correct place to do it. Thank you