C# 如何在空datagridview上显示自定义消息

C# 如何在空datagridview上显示自定义消息,c#,sql-server,winforms,datagridview,C#,Sql Server,Winforms,Datagridview,在我的窗口窗体中,我使用datagridview向用户显示类别的详细信息,但我还想在我的表中找不到记录时显示一条自定义消息,然后我想显示一条自定义消息,如 “没有发现任何记录”。 此消息应该在datagrird视图中,就像您熟悉asp一样,其中有空的数据模板以在gridview 下面是在mydatagridview public void getData() { try { con = new Sql

在我的窗口窗体中,我使用
datagridview
向用户显示类别的详细信息,但我还想在我的表中找不到记录时显示一条自定义消息,然后我想显示一条自定义消息,如 “没有发现任何记录”。 此消息应该在
datagrird
视图中,就像您熟悉
asp
一样,其中有空的
数据模板
以在
gridview
下面是在my
datagridview

public void getData()
        {
            try
            {
                con = new SqlConnection(str);
                con.Open();
                string getAll = "select (CatID) as [ID],CategoryName as [Category Name] from Category order By CategoryName";
                SqlCommand cmd = new SqlCommand(getAll, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "Category");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

and i called this method on form load.

没有实现的方法来做你想做的事情

检查后

if (ds.Rows.Count > 0) 
,您必须在DGV前面放置自己的控件,如标签 或者在DGV上完全从手上提取信息


最简单的方法是只提示一个Message.Box,或者在数据集中现在有行的情况下关闭DGV,并显示另一个带有“No Recores found”消息的控件。

winforms datagridview中没有等效属性。

public void getData()
        {
            try
            {
                con = new SqlConnection(str);
                con.Open();
                string getAll = "select (CatID) as [ID],CategoryName as [Category Name] from Category order By CategoryName";
                SqlCommand cmd = new SqlCommand(getAll, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "Category");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

and i called this method on form load.
你可以研究一下这个问题的解决办法