简体   繁体   English

C#dataGridView不显示我想要显示的信息

[英]C# dataGridView doesnt show information I want it to show

I have two dataGridView tables. 我有两个dataGridView表。 One with Suppliers, second with Products. 一个与供应商,第二与产品。 I want them to work like that: When I click on the row in Suppliers dataGridView, in Products dataGridView it would show only products from selected supplier. 我希望他们像这样工作:当我单击“供应商”数据网格视图中的行时,在“产品”数据网格视图中,它将仅显示来自选定供应商的产品。 This is function I wrote for that purpose: 这是我为此目的编写的函数:

static public void SuppliersProducts(DataGridView _productslist)
   {
       try
       {
           connection.Open();
           SqlCommand commandShow = new SqlCommand("SELECT a.Name FROM Products a INNER JOIN SuppliersProducts b ON a.Id = b.ProductId WHERE b.SupplierId = @SupplierId", connection);
           DataGridViewRow dr1 = _productslist.SelectedRows[0];
           commandShow.Parameters.AddWithValue("@SupplierId", dr1.Cells[0].Value);
           commandShow.ExecuteNonQuery();
       }
       catch (SqlException exception)
       {
           MessageBox.Show(exception.ToString());
       }
       finally
       {
           connection.Close();
       }
   }

Im using it in dataGridView1_CellMouseClick: 我在dataGridView1_CellMouseClick中使用它:

 private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {

        SuppliersProducts(ProductsList);
    }

Where ProductsList is my dataGridView for Products table. 其中ProductsList是我的Products表的dataGridView。 The problem is that it doesnt throw any errors but when I click on certain supplier in my first dataGridView table nothing happens with the second one. 问题是它不会引发任何错误,但是当我在第一个dataGridView表中单击某个供应商时,第二个就没有任何反应。 What am I doing wrong? 我究竟做错了什么?

You can do this: 你可以这样做:

change the CellMouseClick event with CellClick because CellMouseClick fires when any mouse button clicks on cell 使用CellClick更改CellMouseClick事件,因为当任何鼠标按钮单击单元格时CellMouseClick触发CellMouseClick

and the data of sql server should store somewhere 和SQL Server的数据应该存储在某个地方

and ExecuteNonQuery() use for Insert,Delete, Update and the commands that does not return data 和ExecuteNonQuery()用于Insert,Delete,Update和不返回数据的命令

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {

        SuppliersProducts(ProductsList,e.RowIndex);
    }


static public void SuppliersProducts(DataGridView _productslist,int index)
   {
       try
       {
           connection.Open();

           string commandShow=String.Format("SELECT a.Name FROM Products a INNER JOIN SuppliersProducts b ON a.Id = b.ProductId WHERE b.SupplierId = {0}",_productslist.Rows[index].Cells[0].Value));
           //Stroing sql server data
          var dt = new DataTable();
          using (var da = new SqlDataAdapter(commandShow, connection))
              da.Fill(dt);
           foreach(DataRow row in dt.Rows)
           {
                dataGridView2.Rows.Add(row[0],...);
           }
       }
       catch (SqlException exception)
       {
           MessageBox.Show(exception.ToString());
       }
       finally
       {
           connection.Close();
       }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM