Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “显示临时”字样;“未找到任何项目”;DataGridView中的行(如果为空)_C#_Mysql_Winforms - Fatal编程技术网

C# “显示临时”字样;“未找到任何项目”;DataGridView中的行(如果为空)

C# “显示临时”字样;“未找到任何项目”;DataGridView中的行(如果为空),c#,mysql,winforms,C#,Mysql,Winforms,我如何告诉DataGridView,如果它是空的,它应该自动在里面临时添加一个新行,并且它的值应该是“找不到项” 使用消息框可以很容易地解决这个问题,但我不喜欢这种方法 这是我的密码 using (MySqlConnection conn = new MySqlConnection(myConnection)) { string cell = dataGridView3.CurrentCell.Value.ToString(); conn.Open(); string q

我如何告诉DataGridView,如果它是空的,它应该自动在里面临时添加一个新行,并且它的值应该是“找不到项”

使用消息框可以很容易地解决这个问题,但我不喜欢这种方法

这是我的密码

using (MySqlConnection conn = new MySqlConnection(myConnection))
{
    string cell = dataGridView3.CurrentCell.Value.ToString();
    conn.Open();
    string query = "SELECT product_item FROM dequor.prods2,dequor.prods where prods.idprods = prods2.prods_idprods and prod_brand=?para";
    using (MySqlCommand cmd = new MySqlCommand(query, conn))
    {
        cmd.Parameters.AddWithValue("?para", cell);
        try
        {
            sda2 = new MySqlDataAdapter();
            sda2.SelectCommand = cmd;
            datset2 = new DataTable();
            sda2.Fill(datset2);
            bsource2 = new BindingSource();


            bsource2.DataSource = datset2;
            dataGridView2.DataSource = bsource2;
            DataGridViewColumn column = dataGridView2.Columns[0];
            column.Width = 125;
            sda2.Update(datset2);

            if (dataGridView2.RowCount < 1)
            {
                datset2.Clear();
                string row = "NO items found";
                dataGridView2.Rows.Add(row);
             //////got an error here
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("" + ex);
        }
    }
    conn.Close();
}
使用(MySqlConnection conn=newmysqlconnection(myConnection))
{
字符串单元格=dataGridView3.CurrentCell.Value.ToString();
conn.Open();
string query=“从devuor.prods2、devuor.prods中选择产品项目,其中prods.idprods=prods2.prods\u idprods和prod\u品牌=?para”;
使用(MySqlCommand cmd=newmysqlcommand(query,conn))
{
cmd.Parameters.AddWithValue(“?para”,单元格);
尝试
{
sda2=新的MySqlDataAdapter();
sda2.SelectCommand=cmd;
datset2=新数据表();
sda2.填充(datset2);
bsource2=新的BindingSource();
bsource2.DataSource=datset2;
dataGridView2.DataSource=bsource2;
DataGridViewColumn=dataGridView2.Columns[0];
列宽=125;
sda2.更新(datset2);
如果(dataGridView2.RowCount<1)
{
datset2.Clear();
string row=“未找到任何项目”;
dataGridView2.Rows.Add(row);
//////这里有个错误
}
}
捕获(例外情况除外)
{
MessageBox.Show(“+ex”);
}
}
康涅狄格州关闭();
}

以下是我能想到的几个选项

  • 即使没有返回任何记录,
    datset2
    在执行查询后仍应在其中包含一个名为“product\u item”的列,以便可以重复使用该列

    datset2 = new DataTable();
    sda2.Fill(datset2);
    
    if (datset2.Rows.Count == 0)
    {
        // I'm assuming "product_item" is a string
        datset2.Rows.Add("No items found");
    }
    
  • 您可以实例化一个新的
    数据表
    ,并在其中添加所需的列,然后使用它。我不认为这有什么意义,但这是一种选择

    if (datset2.Rows.Count == 0)
    {
        datset2 = new DataTable();
        datset2.Columns.Add("Message", typeof(string));
        datset2.Rows.Add("No items found");
    }
    
  • 您只需将
    标签添加到
    表单
    ,将其放置在
    DataGridView
    上,然后隐藏它,直到需要为止

    labelNoDataMessage.Text = "No items found";
    
    if (datset2.Rows.Count == 0)
    {
        labelNoDataMessage.Show();
    }
    

  • 一些第三方控件可能内置了支持此功能的功能,如Telerik的
    RadGridView
    及其属性。但是,默认的
    DataGridView
    没有这样的属性。

    修改Grant Winney I的答案,最终使用了这个属性

     var dataTable = new DataTable();
     dataTable.Columns.Add("Message", typeof(string));
     dataTable.Rows.Add("No items found");
    
     myDataGridView.DataSource = new BindingSource { DataSource = dataTable };
     myDataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    

    使用标点符号会使你的问题更容易理解。抱歉,我的错误“这里有个错误”——你需要告诉我们错误是什么。编译错误?运行时错误?“当控件绑定数据时,无法通过编程方式将行添加到datagridviews行集合中”。是否有其他方法添加行?@grant tnx我已在您的帮助下解决了我的问题