带 EmptyDataText 属性的 DataGridView 组件 -Windows 窗体应用程序






4.89/5 (6投票s)
在 GridView 控件绑定到不包含任何记录的数据源时,
引言
获取或设置在绑定到不包含任何记录的数据源时呈现的空数据行中显示的文本。
背景
当绑定到控件的数据源不包含任何记录时,空数据行显示在 ASP.NET GridView控件中。使用 EmptyDataText
属性,通过 ASP.NET 中的 EmptyDataRowStyle来指定在空数据行中显示的文本。但 Windows Forms 应用程序中没有这个功能,所以我为此功能创建了一个组件。
Using the Code
以下屏幕显示 EmptyDataText Grid
组件的属性。

绑定包含 0 条记录的网格

绑定网格后,显示如下

以下代码不言自明。以下代码已写入 Component\DataGrid.cs。在 DataGrid.cs 中,我重写了 DataGridView
的 Pint 方法。

以下代码已写入 Form1.cs

关注点
现在我将尝试支持自动标签显示,而不是检查以下代码行
//You should use only following 5 lines of code.
dgEmptyData.DataSource = dt;
if (dt != null && dt.Rows.Count > 0)
//Remove text when records exists
dgEmptyData.EmptyDataText = "";
else
//Assign text when no record found
dgEmptyData.EmptyDataText = txtEmptyText.Text;
历史
如果您发现其中存在任何问题或错误,请留下评论或给我发送电子邮件。如果您对此有任何注释,也请告诉我,这样我就不必重做您的辛苦工作了。
如果您觉得这有帮助,请投一票 “赞”。