0
votes

I have this 2 Datagridviews, dgv1 and 2. How can i check if dgv2 has no "content" or rows?

For example i want to do: Send OrderID: 0001(from dgv1) to archives if dgv 2 is "empty" or no rows? basically i want to remove this Order is its dgv2 has no rows or no products left.

dgv2's content is related to dgv1's primary key 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;
    }
1
Cast your datasource and count that way. For example, if it's a DataTable: DataGridView dgv = new DataGridView(); int i = ((DataTable)dgv.DataSource).Rows.Count; .... If it's something else, cast the same and use the object's "count" function. - Aaron
If you aren't looping, use an if (read.Read()) {. Use sql parameters to avoid sql injection and formatting issues. - LarsTech

1 Answers

1
votes

use the property (dgv2.RowCount > 0)