This is a migrated thread and some comments may be shown as answers.

Hide grid when there is no record in grid.

9 Answers 1437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Abhishek
Top achievements
Rank 1
Abhishek asked on 10 Jan 2012, 01:51 PM
How can i hide the grid when grid is bound to a null collection?

If i set grid visibility to false on grid_NeedDataSource event then no data is displayed even next time when it is bound  with some valid data.
Protected Sub gridConsolidation_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles gridConsolidation.NeedDataSource
    'Get filtered object data whose functional and technical improvement recommended
    Dim filterExpression As String
    filterExpression = String.Format("Opportunity_Id = {0} and Recommendation_Type_Id In ({1})", ddlOpportunity.SelectedValue, _
                                     CType(RecommendationType.Consolidation, Integer))

'dtRecommendationInitiative is a datatable to be used as grid datasource
    Dim dt As DataTable = GetFilteredDataview(dtRecommendationInitiative, filterExpression)
    Dim dataCount As Integer = dt.Rows.Count
    gridConsolidation.VirtualItemCount = dataCount
    gridConsolidation.AllowCustomPaging = Not ShouldApplySortFilterOrGroup(gridConsolidation)
    gridConsolidation.DataSource = dt
 
    'If gridConsolidation.Items.Count > 0 Then
 
    '    divConsolidationGrid.Style.Value = "display:block"
    '    btnMergeToExisting.Visible = True
    '    btnMergeToNew.Visible = True
    '    btnSave.Enabled = True
    '    btnCancel.Enabled = True
    'Else
    '    divConsolidationGrid.Style.Value = "display:none"
    '    btnMergeToExisting.Visible = False
    '    btnMergeToNew.Visible = False
    '    btnSave.Enabled = False
    '    btnCancel.Enabled = False
    'End If
End Sub


Regrds,
Abhishek

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Jan 2012, 02:05 PM
Hello Abhishek,

Try the following code.
C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)    
{    
   if (DataTable1.Rows.Count == 0)    
  {    
    Grid.Visible=false;
  }    
}

-Shinu.
0
Abhishek
Top achievements
Rank 1
answered on 12 Jan 2012, 06:28 AM
But will this reset grid with blank data?
0
Accepted
Andrey
Telerik team
answered on 12 Jan 2012, 07:13 PM
Hi Abhishek,

When RadGrid is bound to a datasource which has no items, the grid is displaying its NoRecordsTemplate if it is enabled.

However, if you want to hide the RadGrid instance you could do it on the PreRender event of RadGrid. In its body you should check what is the count of the items in the Items collection of the MasterTableView and if it is zero you should set the Visible property to false.

Nevertheless, setting Visible property to false will hide the grid, but additionally it will prevent it from rendering on the page at all. If you want to render the grid on the page and then hide it you should use the Display property instead. Namely:

Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    If RadGrid1.MasterTableView.Items.Count = 0 Then
        RadGrid1.Display= False
    End If
End Sub

Give this suggestion a try and check whether this is the expected behavior.

All the best,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Makoto
Top achievements
Rank 1
answered on 28 Jun 2012, 04:37 AM
Where's the Display property for RadGrid? I'm using version 2012.1.411.35 and don't see that property.
0
Andrey
Telerik team
answered on 02 Jul 2012, 12:55 PM
Hi,

Instead of using the Display property it is recommended to use the CssClass property of RadGrid:

RadGrid1.CssClass = "hideGrid";

.hideGrid
{
    display: none;
}

Using this code, RadGrid will be rendered but not be shown on the client. On the other hand if you use the Visible property RadGrid won't be rendered at all.

All the best,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Makoto
Top achievements
Rank 1
answered on 03 Jul 2012, 01:38 AM
Thanks Andrey. I ended up setting the grid's visibility in Page_PreRender so that code-behind can still process the grid.

Do you see any issue with setting the visibility of the grid in Page_PreRender event?
0
Andrey
Telerik team
answered on 03 Jul 2012, 01:33 PM
Hi,

As I stated in my last reply, there is no problem using Visible property to hide RadGrid if you do not intend to use its client-side functionality. This is true because when the Visible property is set to false, no client object are created or sent to the browser, so, there is no way to access them on the client.

On the other hand hiding the Grid with CSS class, the client objects are rendered but they are hidden only in the browser and you could still access them with JavaScript and jQuery.

I hope this helps you to understand the differences between both approaches.

Regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Dimitrios
Top achievements
Rank 1
answered on 02 Jun 2014, 06:41 AM
I used:
.hidegrid {
  display: none;
}

...and in the PreRender event...
    Dim RowCount As Integer = grdDetails.MasterTableView.Items.Count
    If RowCount = 0 Then
      grdDetails.CssClass = "hidegrid"
    Else
      grdDetails.CssClass = "showgrid"
    End If

 ..but although RowCount is zero, the grid is still shown. What is wrong??

(StyleSheet works fine)

 Thanks 







0
Princy
Top achievements
Rank 2
answered on 02 Jun 2014, 09:57 AM
Hi Dimitrios,

The above code works fine at my end, please take a look at the sample code snippet. If it doesn't help, provide your full code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1">
  <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
    <Columns>        
     <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />         
     <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />         
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

VB:
Protected Sub RadGrid1_PreRender(sender As Object, e As System.EventArgs) Handles RadGrid1.PreRender
  Dim count As Integer = RadGrid1.MasterTableView.Items.Count
    If count = 0 Then
      RadGrid1.CssClass = "hideGrid"
    End If
End Sub

CSS:
<style type="text/css">
    .hideGrid
    {
        display: none;
    }
</style>

Thanks,
Princy
Tags
Grid
Asked by
Abhishek
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Abhishek
Top achievements
Rank 1
Andrey
Telerik team
Makoto
Top achievements
Rank 1
Dimitrios
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or