2017-02-16 5 views
0

私はこの2 Datagridviews、dgv1と2を持っています。どのように私はdgv2に "内容"または行がないかどうか確認できますか?Datagridviews行数?

たとえば、次のようにします。dgv 2が "空"または行がない場合、OrderID:0001(dgv1から)をアーカイブに送信しますか?基本的に私はこの注文を取り除きたいのですが、そのdgv2には行がないか、商品が残っていません。

dgv2のコンテンツはdgv1の主キーbtwに関連しています。 enter image description here

 private void dgvReceiving_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True")) 
     { 
      SqlCommand command = 
      new SqlCommand("select OrderID,SupplierName,LeadTime,OrderedBy,DateOrdered,Status,DateToReceived from Orders where OrderID = '" + dgvReceiving.CurrentRow.Cells[0].Value.ToString() + "'", connection); 
      connection.Open(); 

      SqlDataReader read = command.ExecuteReader(); 

      while (read.Read()) 
      { 
       rorderid.Text = (read["OrderID"].ToString()); 
       rsupplier.Text = (read["SupplierName"].ToString()); 
       rleadtime.Text = (read["LeadTime"].ToString()); 
       rordered.Text = (read["OrderedBy"].ToString()); 
       rdateordered.Text = (read["DateOrdered"].ToString()); 
       rdatedelivery.Text = (read["DateToReceived"].ToString()); 
       rstatus.Text = (read["Status"].ToString()); 

      } 


      SqlConnection cn2 = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True"); 
      cn2.Open(); 
      string amt = "select sum(TotalPrice) from Orders_productholder where OrderID = '" + rorderid.Text + "'"; 
      SqlCommand cmd2 = new SqlCommand(amt, cn2); 
      labelsupertotal.Text = "P "+cmd2.ExecuteScalar().ToString(); 
     } 

     dgvreceivingproduct(); 

    } 

    private void dgvreceivingproduct() 
    { 
     SqlConnection cn3 = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True"); 
     cn3.Open(); 
     string qry = "Select Status,ID,ProductID,ProductName,Dosage,Price,QtyOrdered,TotalPrice,ExpirationDate,SellingPrice,BatchNumber from Orders_productholder where Status = 'Unreceived' and OrderID = '" + dgvReceiving.CurrentRow.Cells[0].Value.ToString() + "' "; 
     SqlCommand cmd3 = new SqlCommand(qry, cn3); 
     DataTable poholder = new DataTable(); 
     SqlDataAdapter adapter = new SqlDataAdapter(cmd3); 
     adapter.Fill(poholder); 

     dgvReceivingproducts.DataSource = poholder; 
    } 
+0

があなたのデータソースをキャストし、そのようにカウントプロパティを使用します。たとえば、DataTableの場合:DataGridView dgv = new DataGridView(); int i =((DataTable)dgv.DataSource).Rows.Count; ....それが他のものなら、同じものをキャストし、オブジェクトの "count"関数を使用します。 – Aaron

+0

ループしていない場合は、 'if(read.Read()){'を使用してください。 SQLインジェクションと書式設定の問題を回避するには、sqlパラメータを使用します。 – LarsTech

答えて

0

(dgv2.RowCount > 0)

+0

空のグリッドには、まだ「新しい行」の行が表示されている可能性があります。行は数えられます。 – LarsTech