windowsforms-3036-controls-and-libraries-data-grid-examples-painting-how-to-display-some-text-when-no-record-is-displayed-in-the-grid.md
The code below employs the ColumnView.CustomDrawEmptyForeground event to draw a custom string within the Data Grid control area when the Grid has no records to display. Clicking the “Try Searching Again” link displays a dialog that allows end-users to modify the currently applied filter.
private void Form3_Load1(object sender, EventArgs e) {
CustomDrawEmptyForeground(gridControl1, gridView1);
}
public static void CustomDrawEmptyForeground(GridControl gridControl, GridView gridView) {
string searchName = string.Empty;
gridView.ActiveFilterCriteria = new BinaryOperator("Category_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;
//Handle this event to paint View's empty space in a custom manner
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 DevExpress.Data.Filtering.BinaryOperator("Category_Name", searchName);
}
};
}
Private Sub Form3_Load1(ByVal sender As Object, ByVal e As EventArgs)
CustomDrawEmptyForeground(gridControl1, gridView1)
End Sub
Public Shared Sub CustomDrawEmptyForeground(ByVal gridControl As GridControl, ByVal gridView As GridView)
Dim searchName As String = String.Empty
gridView.ActiveFilterCriteria = New BinaryOperator("Category_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
'Handle this event to paint View's empty space in a custom manner
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 DevExpress.Data.Filtering.BinaryOperator("Category_Name", searchName)
End If
End Sub
End Sub