All posts

Empty data template in RadGridView for Silverlight (and WPF)

One more RadGridView behavior frequently requested by our clients is to display a message when there are no records to display. A typical real life scenario would be a RadGridView displaying a list of search results. When there are no results found we would like to have a message like “No results found!” displayed in RadGridView.

 

noresults

 

 

If you download the project above , you will see that if you get the EmptyDataTemplateBehavior.cs into your project, setting the empty data template will be as easy as writing a few lines of XAML :

<telerik:RadGridView x:Name="RadGridView1" >
			<i:Interaction.Behaviors>
				<local:EmptyDataTemplateBehavior>
					<local:EmptyDataTemplateBehavior.EmptyDataTemplate>
						<DataTemplate>
							<TextBlock Text="No results found!" Foreground="White"  HorizontalAlignment="Center"  VerticalAlignment="Center" Visibility="{Binding MessageVisibility}" />
						</DataTemplate>
					</local:EmptyDataTemplateBehavior.EmptyDataTemplate>
				</local:EmptyDataTemplateBehavior>
			</i:Interaction.Behaviors>
		</telerik:RadGridView>

 

*As you may know from my previous posts concerning attached behaviors , you will need  to add a reference to the System.Windows.Interactivity.dll.

*As always the same approach is applicable for the WPF version of RadGridView provided that you reference the right version of the System.Windows.Interactivity.dll.

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Comments  3

  • 11 Feb 2010

    I get an:
    ArgumentOutOfBoundsException

    on the row:

    Grid

     

     

    rootGrid = gridView.ChildrenOfType<Grid>()[1];

     

    in EmptyDataTemplateBehavior

    And if I catch it I don't get the "No result found" text

    Fredrik Lundberg

  • 08 Mar 2010

    Hello,

    Any solution on 'ArgumentOutOfBoundsException' ??

    Priya

  • 16 Mar 2010

    The function containing the ChildrenOfType line is called twice. The first time there are no children of type Grid.

    Changing the function to the following solves the problem and displays the empty message, but I can't explain why. Perhaps someone of telerik can post the correct solution.

     

    private

     

    void LoadTemplateIntoGridView(RadGridView gridView)

     

    {

     

    contentPresenter.IsHitTestVisible =

    false;

     

    contentPresenter.DataContext =

    this;

     

    contentPresenter.ContentTemplate =

    this.EmptyDataTemplate;

     

     

    if (gridView.ChildrenOfType<Grid>().Count > 0)

     

    {

     

    Grid rootGrid = gridView.ChildrenOfType<Grid>()[1];

     

    contentPresenter.SetValue(

    Grid.RowProperty, 2);

     

    contentPresenter.SetValue(

    Grid.RowSpanProperty, 2);

     

    contentPresenter.SetValue(

    Grid.ColumnSpanProperty, 2);

     

    contentPresenter.SetValue(

    Border.MarginProperty, new Thickness(0, 27, 0, 0));

     

    rootGrid.Children.Add(contentPresenter);

    }

    }

    Ralph

Post a comment
  1. Formatting options
       
     
     
     
     
       
  2. Security image