如何在Windows应用程序中为DataGridView创建EmptyDataText [英] How to creating EmptyDataText for DataGridView in Windows Application

查看:198
本文介绍了如何在Windows应用程序中为DataGridView创建EmptyDataText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我面临的问题是根据数据源显示/隐藏标签。如果数据源没有行,那么我想在winforms应用程序中设置No Data Found否则显示记录数

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.

任何解决方案将不胜感激!
谢谢,
Imdadhusen

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.

谢谢,
Imdadhusen

Thanks, Imdadhusen

这篇关于如何在Windows应用程序中为DataGridView创建EmptyDataText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆