Click here to Skip to main content
12,549,181 members (49,369 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
i use this code to delete selected datagrid from data base but it is not delete

for (int i = 0; i < dataGridView1.Rows.Count; i++)
   DataGridViewRow dr = dataGridView1.Rows[i];
   if (dr.Selected == true)
         SqlCommand cmd = new SqlCommand("Delete from calender where ID= ' " + i + "'", con);     
      catch (Exception ex)

[edit]code block added and indexation fixed[/edit]
Posted 16-Nov-12 22:05pm
Updated 16-Nov-12 23:09pm
faisal23 17-Nov-12 4:15am
windows / web
n.podbielski 17-Nov-12 6:11am
No problem? No error?
meme_307 17-Nov-12 6:33am
i want to delete data from data grid
the not deleted from database
i worked in windows
i make button to delete the selected row from grid ,, it delete just from grid not data base
pryashrma 17-Nov-12 7:08am
does value of database field "ID" and variable "i" are same?
meme_307 17-Nov-12 7:26am
no it is not the same so it is not delete ,,,
now i need delete code to delete the selected row from database
meme_307 17-Nov-12 7:33am
the id not necessary in data base i cane delete it but i need delete the selected row from data grid

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Can you try after removing space between single quotes and double quotes from query.

SqlCommand cmd = new SqlCommand("Delete from calender where ID= '" + i + "'", con);
meme_307 17-Nov-12 6:53am
i tried it's not delete from database
Dominic Abraham 17-Nov-12 7:06am
Can you try directly removing the value from database after giving a value for i. I.E . Execute the query
Delete from calender where ID = '1' in database. Just to make sure that, there is no issues with your query/data. And if you are able to delete the data, just try the same query from code [after inserting the deleted data]


SqlCommand cmd = new SqlCommand("Delete from calender where ID= '1'", con);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100