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