HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Thursday, July 19, 2012

WinForms Grid - How to Display a Custom String When No Record is Displayed in the Grid

Sometimes it can be useful to inform your user why there aren’t any records displayed in your grid. For example, if the user applies a custom filter that doesn’t return any records, then nothing is displayed within the grid view.

In this video, you will learn how to custom draw a string when no records are being displayed.

So let’s get started…

1.       Starting from a new WinForms application, I drop an XtraGrid control onto the form and dock it to its parent.

2.       I’ll bind the grid to a data source by invoking its smart tag and clicking the “Add Project Data Source” link.

3.       I’ll use an existing Connection String for the CarsDB database.

4.       I click “Next” and select the “Cars” Table that will supply data to the grid.

5.       Finally, I click finish to complete the wizard.

6.       I run the Grid’s designer and switch to the columns page to remove the unnecessary columns for this example.

7.       Next, I set the OptionsView.ShowFooter property to “True” and the ShowFilterPanelMode property to “ShowAlways”.

8.       It is possible to apply filters without this setting, but for the purposes of this lesson, I want the panel to be always visible.

9.       Now I’m going to add the code to display a string when there are no records visible.

10.   To do this, I create a handler for the View’s CustomDrawEmptyForeground event.

11.   This event is raised before a View is painted assuming that there aren’t any rows being displayed. Keep in mind that this event is fired only for the grid control’s MainView.

12.   First, I’m going to add references to the following namespace.

13.   Back in the event handler, I add the following code.

14.   Here I check for two conditions: If a data source is empty and if no records are being displayed due to the application of filter criteria.

15.   And I’m done!

16.   I run the application to see the results.

17.   I open the Filter Editor and create a new filter.

18.   For this example, I want to show all the cars that have 99 cylinder engines.

19.   We know very well that the data doesn’t exist, so no records should be displayed when I apply the filter.

20.   I click on OK.

21.   Previously, I would’ve been shown an empty grid view, but notice the user-friendly message informing me that there aren’t any cars with 99 cylinder engines available.

22.   Now I need to look somewhere else for my vehicle purchase.

 

No comments:

Post a Comment