Reputation: 35
How can I show a text in the grey part of a DataGridView
when it's empty.
I found this example but not work in VB.net
GridView1.EmptyDataText="No Records Found";
Upvotes: 2
Views: 2136
Reputation: 125197
EmptyDataText
is a property of Web Forms GridView
control. In Windows Forms, to show a text when DataGridView
doesn't have any row, you need to render the text yourself. To do so, you can handle Paint
event of DataGridView
and render the text using TextRenderer.DrawText
.
C#
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
if (dataGridView1.Rows.Count == 0)
TextRenderer.DrawText(e.Graphics, "No records found.",
dataGridView1.Font, dataGridView1.ClientRectangle,
dataGridView1.ForeColor, dataGridView1.BackgroundColor,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
VB.NET
Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) _
Handles DataGridView1.Paint
If DataGridView1.Rows.Count = 0 Then
TextRenderer.DrawText(e.Graphics, "No records found.",
DataGridView1.Font, DataGridView1.ClientRectangle,
DataGridView1.ForeColor, DataGridView1.BackgroundColor,
TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
End If
End Sub
Upvotes: 3