I am trying to delete record from datagridview .. record is deleted successfully when I delete record remove from database but not from grid means grid not refreshed so how I refresh that I use this:
private void button8_Click(object sender, EventArgs e)
{
con.Open();
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
var row = dataGridView1.SelectedRows[0];
string sql = " delete from CustomerOrder where orderid = '" + dataGridView1.CurrentRow.Cells[0].Value + "'";
SqlCommand cmd = new SqlCommand(sql, con);
this.dataGridView1.Rows.Remove(row);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Record Deleted successfully...");
dataGridView1.Refresh();
con.Close();
}
records
Orderid ItemName DateCurrent Quantity ItemCost bill |
100 fanta 2017-04-16 14:33:49.600 4 10 40 |
101 fanta 2017-04-16 14:35:45.823 12 10 120 |
102 fanta 2017-04-16 14:35:45.823 12 10 10 |
NULL NULL NULL NULL NULL 170 |
when i try to remove delete record from datagridview then record is removed from grid also from database but then end total sum is total effected in datagrid i.e. if i remove record 102 then 170 should be change to 160 ... i use this sql query for display this is working when i check this in sql but when i delete record then 170 fig not change
select c.Orderid,i.ItemName,c.DateCurrent,c.Quantity,c.ItemCost,i.ItemCost*c.quantity as bill
from CustomerOrder c
inner join item i on i.ItemId=c.ItemId
inner join userinformation ui on ui.userid=c.userid
where c.userid=2003
union all
select null,null,null,null,null,sum (i.ItemCost*c.Quantity) bill
from CustomerOrder c
inner join item i on i.itemid=c.ItemId
inner join userinformation ui on ui.userid=c.userid
where c.userid=2003