How to create an EmptyDataText for a DataGridView in a Windows application

Today I ran into a problem to show / hide the label according to the data source. If the data source does not have a row, then I would like to set "No data" , otherwise display the number of records in the winforms application .

This is possible in Asp.net, for example:

<emptydatatemplate>
No Data Found
</emptydatatemplate>

OR

EmptyDataText=" No Data Found"

But I would like in a Windows Application. Please help me if you have a solution for this.

Any solution would be appreciated! Thanks, Imdadhusen

+3
source share
2 answers

One way to achieve this is to use the Paint () event to check the lines, and if they are not there, write your message: Collapse

private void dataGridView1_Paint ( object sender, PaintEventArgs e )
{
    DataGridView sndr = ( DataGridView )sender;

    if ( sndr.Rows.Count == 0 ) // <-- if there are no rows in the DataGridView when it paints, then it will create your message
    {
        using ( Graphics grfx = e.Graphics )
        {
            // create a white rectangle so text will be easily readable
            grfx.FillRectangle ( Brushes.White, new Rectangle ( new Point (), new Size ( sndr.Width, 25 ) ) );
            // write text on top of the white rectangle just created
            grfx.DrawString ( "No data returned", new Font ( "Arial", 12 ), Brushes.Black, new PointF ( 3, 3 ) );
        }
    }
}

JOAT-MON .

, Imdadhusen

+10

Paint, , , , , ( ), .

0

All Articles