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

标签 c# winforms datagrid .net-2.0

我有一个数据网格,当用户将文件拖放到它上面时,它会填充 CSV 数据。是否可以在空白网格中显示一条消息,例如“请将文件拖到此处”或“此网格当前为空”。当我等待文件被拖动以设置列等时,网格当前显示为深灰色框。

最佳答案

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

关于c# - 是否可以在空数据网格中显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/530130/

相关文章:

c# - 我如何检测连接到电脑 USB 的数码相机?还有相机名称

c# - DataGrid:如何操作所选项目

c# - WPF DataGrid EditItem 异常

c# - Rebus 和 WebApi

winforms - 当 AxWindowsMediaPlayer 关闭时,出现 AccessViolation 异常

c# - System.PlatformNotSupportedException 仅在作为 docker 镜像运行时引用 System.Data.SqlClient

c# - 如何将 MinorGrid 行数固定为 Microsoft .NET 4.0 Chart 中的固定数字?

wpf - 如何更改 Datagrid 滚动条的大小?

c# - 来自 WCF 服务的动态 XML/JSON

c# - 如何在 ASP.NET Core 中启用 CORS