如何在 Windows 应用程序中为 DataGridView 创建 EmptyDataText [英] How to creating EmptyDataText for DataGridView in Windows Application
问题描述
今天我面临着根据数据源显示/隐藏标签的问题.如果数据源没有行,则我想设置未找到数据",否则显示在winforms应用程序中的记录数.
Today i am facing problem to show/hide label according to data source. If the data source has no row then i would like to set "No Data Found" else display number of records in winforms application.
这在 Asp.net 中是可能的,例如:
This would be possible in Asp.net like:
<emptydatatemplate>
No Data Found
</emptydatatemplate>
或
EmptyDataText=" No Data Found"
但我想在 Windows 应用程序中.如果您有任何相同的解决方案,请帮助我.
But I would like in Windows Application. Please help me if you have any solution for the same.
任何解决方案将不胜感激!谢谢,伊姆达胡森
Any solution would be appreciated! Thanks, Imdadhusen
推荐答案
实现此目的的一种方法是使用 Paint() 事件检查行,如果没有,则写下您的消息:收起
One way you could accomplish this is to use the Paint() event to check the rows and if there are none, then write your message: Collapse
private void dataGridView1_Paint ( object sender, PaintEventArgs e )
{
DataGridView sndr = ( DataGridView )sender;
if ( sndr.Rows.Count == 0 ) // <-- if there are no rows in the DataGridView when it paints, then it will create your message
{
using ( Graphics grfx = e.Graphics )
{
// create a white rectangle so text will be easily readable
grfx.FillRectangle ( Brushes.White, new Rectangle ( new Point (), new Size ( sndr.Width, 25 ) ) );
// write text on top of the white rectangle just created
grfx.DrawString ( "No data returned", new Font ( "Arial", 12 ), Brushes.Black, new PointF ( 3, 3 ) );
}
}
}
感谢 JOAT-MON 接受解决方案.
Thanks JOAT-MON for accepted solution.
谢谢,伊姆达胡森
这篇关于如何在 Windows 应用程序中为 DataGridView 创建 EmptyDataText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!