【问题标题】:how to show custom message on empty datagridview如何在空的datagridview上显示自定义消息
【发布时间】:2023-03-23 17:46:02
【问题描述】:

在我的窗口表单中,我使用datagridview 向用户显示类别的详细信息,但我还想在我的表中没有找到记录时显示自定义消息,然后我想显示自定义消息,例如 “没有找到记录”。 此消息应该在 datagrird 视图中,就像您熟悉 asp 一样,其中有空的 data template 以在 gridview 中显示自定义消息 这是在我的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.

【问题讨论】:

    标签: c# sql-server winforms datagridview


    【解决方案1】:

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

    检查后

    if (ds.Rows.Count > 0) 
    

    ,无论哪种方式,您都必须在 DGV 前面放置一个自己的控件,例如标签 或者您完全从 DGV 上获取您的信息。

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

    【讨论】:

      【解决方案2】:

      winforms datagridview 中没有等效的属性

      您可能会研究此question 的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-30
        • 1970-01-01
        • 2022-01-20
        相关资源
        最近更新 更多