我有一个数据网格,当用户将文件拖放到它上面时,它会填充 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/