Winforms 如何在Windows应用程序中为DataGridView创建EmptyDataText

Winforms 如何在Windows应用程序中为DataGridView创建EmptyDataText,winforms,datagridview,custom-controls,Winforms,Datagridview,Custom Controls,今天,我面临根据数据源显示/隐藏标签的问题。如果数据源没有行,则我想设置“未找到数据”否则在winforms应用程序中显示记录数 这在Asp.net中是可能的,如: <emptydatatemplate> No Data Found </emptydatatemplate> 但我想在Windows应用程序中。如果你有任何相同的解决方案,请帮助我 任何解决方案都将不胜感激! 谢谢 Imdadhusen实现这一点的一种方法是使用Paint()事件检查行,如果没有行,则编写消

今天,我面临根据数据源显示/隐藏标签的问题。如果数据源没有行,则我想设置“未找到数据”否则在winforms应用程序中显示记录数

这在Asp.net中是可能的,如:

<emptydatatemplate>
No Data Found
</emptydatatemplate>
但我想在Windows应用程序中。如果你有任何相同的解决方案,请帮助我

任何解决方案都将不胜感激! 谢谢
Imdadhusen实现这一点的一种方法是使用Paint()事件检查行,如果没有行,则编写消息: 崩溃

private void dataGridView1_Paint(对象发送方,PaintEventArgs e)
{
DataGridView sndr=(DataGridView)发送方;

如果(sndr.Rows.Count==0)/因为我在使用Paint事件实现此行为时遇到问题,我通过向表单添加一个面板来解决问题,该面板包含在没有显示数据时要显示的图形(基本上是两个标签),并在需要时将其与网格交换

EmptyDataText=" No Data Found"
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 ) );
        }
    }
}