是否可以在空数据网格中显示消息

Ady*_*emp 8 c# datagrid .net-2.0 winforms

我有一个datagrid,当用户将文件拖放到其上时,该数据网格填充了CSV数据.是否可以在空白网格中显示消息,例如"请在此处拖动文件"或"此网格当前为空".当我等到文件被拖动以设置列等时,网格当前显示为深灰色框.

geo*_*tnz 8

我们继承了DataGridView控件并添加了它.我们不需要拖放功能 - 我们只需要告诉用户何时没有从查询中返回数据.

我们有一个像这样声明的emptyText属性:

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

并重载了PaintBackground函数:

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)