Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display a message in an empty datagrid

I have a datagrid which is populated with CSV data when the user drag/drops a file onto it. Is it possible to display a message in the blank grid for example "Please drag a file here" or "This grid is currently empty". The grid currently displays as a dark grey box as I wait until the file is dragged to setup the columns etc.

like image 780
Ady Kemp Avatar asked Feb 09 '09 21:02

Ady Kemp


1 Answers

We subclassed the DataGridView control and added this. We didnt need the drag/drop functionality - we just needed to tell the user when there was no data returned from their query.

We have an emptyText property declared like this:

    private string cvstrEmptyText = "";
    [Category("Custom")]
    [Description("Displays a message in the DataGridView when no records are displayed in it.")]
    [DefaultValue(typeof(string), "")]
    public string EmptyText
    {
        get
        {
            return this.cvstrEmptyText;
        }
        set
        {
            this.cvstrEmptyText = value;
        }
    }

and overloaded the PaintBackground function:

    protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
    {
        RectangleF ef;
        base.PaintBackground(graphics, clipBounds, gridBounds);
        if ((this.Enabled && (this.RowCount == 0)) && (this.EmptyText.Length > 0))
        {
            string emptyText = this.EmptyText;
            ef = new RectangleF(4f, (float)(this.ColumnHeadersHeight + 4), (float)(this.Width - 8), (float)((this.Height - this.ColumnHeadersHeight) - 8));
            graphics.DrawString(emptyText, this.Font, Brushes.LightGray, ef);
        }
    }
like image 72
geofftnz Avatar answered Oct 06 '22 10:10

geofftnz