windowsforms-548-controls-and-libraries-data-grid-visual-elements-grid-view-elements-empty-space.md
Empty space appears when cells and (optionally) group rows do not occupy an entire View’s area. This can occur when there are few rows, total column widths are less than a View’s width or as a result of vertical scrolling.
The GridViewAppearances.Empty property allows you to modify this area’s appearance. You can also handle the CustomDrawEmptyForeground event to manually paint this region. The “CustomDrawEmptyForeground” module in the DevExpress Demo Center illustrates how to add a clickable link to the empty space area. This link is shown when a Data Grid filter returns no results, and allows users to modify the filter condition.
Run Demo: CustomDrawEmptyForeground
string searchName = string.Empty;
gridView.ActiveFilterCriteria = new BinaryOperator("Name", searchName);
// Initialize variables used to paint View's empty space in a custom manner
Font noMatchesFoundTextFont = new Font("Tahoma", 10);
Font trySearchingAgainTextFont = new Font("Tahoma", 15, FontStyle.Underline);
Font trySearchingAgainTextFontBold = new Font(trySearchingAgainTextFont, FontStyle.Underline | FontStyle.Bold);
SolidBrush linkBrush = new SolidBrush(DevExpress.Skins.EditorsSkins.GetSkin(
DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveLookAndFeel).Colors["HyperLinkTextColor"]);
string noMatchesFoundText = "No matches found";
string trySearchingAgainText = "Try searching again";
Rectangle noMatchesFoundBounds = Rectangle.Empty;
Rectangle trySearchingAgainBounds = Rectangle.Empty;
bool trySearchingAgainBoundsContainCursor = false;
int offset = 10;
gridView.CustomDrawEmptyForeground += (s, e) => {
e.DefaultDraw();
e.Appearance.Options.UseFont = true;
e.Appearance.Font = noMatchesFoundTextFont;
//Draw the noMatchesFoundText string
Size size = e.Appearance.CalcTextSize(e.Cache, noMatchesFoundText, e.Bounds.Width).ToSize();
int x = (e.Bounds.Width - size.Width) / 2;
int y = e.Bounds.Y + offset;
noMatchesFoundBounds = new Rectangle(new Point(x, y), size);
e.Appearance.DrawString(e.Cache, noMatchesFoundText, noMatchesFoundBounds);
//Draw the trySearchingAgain link
e.Appearance.Font = trySearchingAgainBoundsContainCursor ?
trySearchingAgainTextFontBold : trySearchingAgainTextFont;
size = e.Appearance.CalcTextSize(e.Cache, trySearchingAgainText, e.Bounds.Width).ToSize();
x = noMatchesFoundBounds.X - (size.Width - noMatchesFoundBounds.Width) / 2;
y = noMatchesFoundBounds.Bottom + offset;
size.Width += offset;
trySearchingAgainBounds = new Rectangle(new Point(x, y), size);
e.Appearance.DrawString(e.Cache, trySearchingAgainText, trySearchingAgainBounds, linkBrush);
};
gridView.MouseMove += (s, e) => {
trySearchingAgainBoundsContainCursor = trySearchingAgainBounds.Contains(e.Location);
gridControl.Cursor = trySearchingAgainBoundsContainCursor ? Cursors.Hand : Cursors.Default;
gridView.InvalidateRect(trySearchingAgainBounds);
};
gridView.MouseDown += (s, e) => {
if(trySearchingAgainBoundsContainCursor) {
searchName = XtraInputBox.Show(string.Format("Enter {0}", "Name"), string.Format(
"Enter {0} dialog", "Name"), searchName);
gridView.ActiveFilterCriteria = new BinaryOperator("Name", searchName);
}
};
Dim searchName As String = String.Empty
gridView.ActiveFilterCriteria = New BinaryOperator("Name", searchName)
' Initialize variables used to paint View's empty space in a custom manner
Dim noMatchesFoundTextFont As New Font("Tahoma", 10)
Dim trySearchingAgainTextFont As New Font("Tahoma", 15, FontStyle.Underline)
Dim trySearchingAgainTextFontBold As New Font(trySearchingAgainTextFont, FontStyle.Underline Or FontStyle.Bold)
Dim linkBrush As New SolidBrush(DevExpress.Skins.EditorsSkins.GetSkin(DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveLookAndFeel).Colors("HyperLinkTextColor"))
Dim noMatchesFoundText As String = "No matches found"
Dim trySearchingAgainText As String = "Try searching again"
Dim noMatchesFoundBounds As Rectangle = Rectangle.Empty
Dim trySearchingAgainBounds As Rectangle = Rectangle.Empty
Dim trySearchingAgainBoundsContainCursor As Boolean = False
Dim offset As Integer = 10
AddHandler gridView.CustomDrawEmptyForeground, Sub(s, e)
e.DefaultDraw()
e.Appearance.Options.UseFont = True
e.Appearance.Font = noMatchesFoundTextFont
'Draw the noMatchesFoundText string
Dim size As Size = e.Appearance.CalcTextSize(e.Cache, noMatchesFoundText, e.Bounds.Width).ToSize()
Dim x As Integer = (e.Bounds.Width - size.Width) \ 2
Dim y As Integer = e.Bounds.Y + offset
noMatchesFoundBounds = New Rectangle(New Point(x, y), size)
e.Appearance.DrawString(e.Cache, noMatchesFoundText, noMatchesFoundBounds)
'Draw the trySearchingAgain link
e.Appearance.Font = If(trySearchingAgainBoundsContainCursor, trySearchingAgainTextFontBold, trySearchingAgainTextFont)
size = e.Appearance.CalcTextSize(e.Cache, trySearchingAgainText, e.Bounds.Width).ToSize()
x = noMatchesFoundBounds.X - (size.Width - noMatchesFoundBounds.Width) \ 2
y = noMatchesFoundBounds.Bottom + offset
size.Width += offset
trySearchingAgainBounds = New Rectangle(New Point(x, y), size)
e.Appearance.DrawString(e.Cache, trySearchingAgainText, trySearchingAgainBounds, linkBrush)
End Sub
AddHandler gridView.MouseMove, Sub(s, e)
trySearchingAgainBoundsContainCursor = trySearchingAgainBounds.Contains(e.Location)
gridControl.Cursor = If(trySearchingAgainBoundsContainCursor, Cursors.Hand, Cursors.Default)
gridView.InvalidateRect(trySearchingAgainBounds)
End Sub
AddHandler gridView.MouseDown, Sub(s, e)
If trySearchingAgainBoundsContainCursor Then
searchName = XtraInputBox.Show(String.Format("Enter {0}", "Name"), String.Format("Enter {0} dialog", "Name"), searchName)
gridView.ActiveFilterCriteria = New BinaryOperator("Name", searchName)
End If
End Sub
See Also