1

I've a GridView inside a Panel that I want to hide when the child is empty because at the moment remains a fieldset with the legend text and nothing inside.

I've already tried to put something like Panel.Visible = GridView.Rows.Count > 0 in the Page_Load event but it doesn't works well.

How can I obtain the result that I need?

Thank you

Some more details:

If on first load the database table is empty I don't see the fieldset, when I add a row the Panel with the GridView doesn't appears; if on first load I have a row I can see the Panel with the GridView, when I delete the unique row anything disapears but never come back even if I insert a new row. I think that the Page_Load is not the right event.

chenny
  • 769
  • 2
  • 17
  • 44

4 Answers4

2

try this..

Panel.Visible = (GridView.Rows.Count > 0?false:true);

Dhaval
  • 2,341
  • 1
  • 13
  • 16
  • Put colon `:` instead of the semi-colon `;`. – Dhrumil Jun 12 '15 at 08:58
  • Please Check Edited Answer..!! – Dhaval Jun 12 '15 at 08:59
  • Hello, I've already tried this code and I get Always the same result: if on first load the database table is empty I don't see the `fieldset`, when I add an row the Panel with the GridView doesn't appears; if on first load I have a row I can see the Panel with the GridView, when I delete the unique row anything disapears but never come back even if I insert a new row. I think that the Page_Load is not the right event. – chenny Jun 12 '15 at 09:13
1

Please try following steps:

  1. Check for Panel.Visible = GridView.Rows.Count > 0 in a late page event such as Page_PreRender
  2. Add OnRowDeleted event in GridView and check for Panel.Visible = GridView.Rows.Count > 0 if the row deleting is a last row. Also rebind the GridView as GridView.DataBind()
  3. Add OnRowCreated event in GridView and repeat the same process as you did in case of row deletion.

Code:

protected void Page_PreRender(object sender, EventArgs e)
{
    Panel.Visible = GridView.Rows.Count > 0;
}

protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
    GridView.DataBind();
    Panel.Visible = GridView.Rows.Count > 0;

}
protected void GridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
    GridView.DataBind();
    Panel.Visible = GridView.Rows.Count > 0;
}

I hope this will solve your issue.

ManP
  • 231
  • 1
  • 6
1

In the end I found my problem... I've put the FormView for the insert and the GridView in different UpdatePanels, so unified all in the same UpdatePanel and used this code:

Panel.Visible = GridView.Rows.Count > 0;

in the GridView DataBound event and it worked.

Thank you everyone.

chenny
  • 769
  • 2
  • 17
  • 44
0

If you simply want to make your panel visible/invisible based on the Gridview Rows, then its as simple as this :

if(GridView.Rows.Count > 0)
    PanelId.Visible = true;
else
    PanelId.Visible = false;

But make sure you do this code after you have called the Gridview binding function.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • which is exactly what he already wrote, just in a more extensive way – LostPhysx Jun 12 '15 at 08:58
  • Hello, the GridView is binded by an ObjectDataSource without any code behind and so I don't know how can I put after binding. Thank you – chenny Jun 12 '15 at 09:08
  • After deleting or adding a row, you will have to make sure to check the condition again. `Page_Load` will work. Just check your condition on postback and rebind the GridView to update the row status. – Dhrumil Jun 12 '15 at 09:15