当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《是否可以在空数据网格中显示消息》经验,为你挑选了1个好方法。

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



1> geofftnz..:

我们继承了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;
        }
    }

并重载了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);
        }
    }

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有