views:

1072

answers:

4

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.

+1  A: 

I think the easiest thing to do here is to make a giant label control to do the "Drag Here" and handle the label's drag/drop event. Once the drag/drop is complete, hide the label and show the grid.

routeNpingme
I've used this technique with ListViews in the past, nice and clean and simple!
Richard Ev
+1  A: 

if you use a gridview instead you can use the EmptyDataText property. It can do everything that a datagrid can and (IMHO) I think it is easier to work with in most cases

jmein
This could be a solution but I'm using Winforms not ASP. I will need to investigate this
Ady Kemp
+1  A: 

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);
        }
    }
geofftnz
This is nice and reusable with no extra logic, I can set the emptytext in the designer
Ady Kemp
A: 

What I do in this situation is add a tab control to the form, put the DGV in one tab, and a label ('Drag Here' or something similar) in the another tab. Hide the tabs. Then if the DGV is empty, show the tab with the label. Just like routeNpingme's answer you would handle the drag/drop event here, load the DGV in the background, and then switch tabs when it is finished loading. This is nice, because you also have the ability to easily switch back and forth between the tabs while in VS Designer.

Rob Lund