У меня есть это 2 Datagridviews, dgv1 и 2. Как я могу проверить, нет ли у dgv2 «контента» или строк?Datagridviews row count?
Например, я хочу: Отправить OrderID: 0001 (от dgv1) в архивы, если dgv 2 «пуст» или нет строк? в основном я хочу удалить этот заказ, его dgv2 не имеет ни строк, ни продуктов.
Содержание dgv2 связано с первичным ключом dgv1.
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;
}
Отдайте свой источник данных и считать, что путь. Например, если это DataTable: DataGridView dgv = new DataGridView(); int i = ((DataTable) dgv.DataSource) .Rows.Count; .... Если это что-то другое, примените то же самое и используйте функцию «count» объекта. – Aaron
Если вы не зацикливаете, используйте 'if (read.Read()) {'. Используйте sql-параметры, чтобы избежать проблем с sql-инъекциями и форматированием. – LarsTech