I want to delete records from database in datagridview on window form app.
here is my code
System.Data.SqlClient.SqlCommandBuilder cb2;
cb2 = new System.Data.SqlClient.SqlCommandBuilder(da);
DataTable dt2 = ds.Tables["Employee"];
dt.PrimaryKey = new DataColumn[] { dt.Columns["EmpID"] };
dtDelete.Delete();
da.Update(ds, "Employee");
MessageBox.Show("Record Deleted");
int inc = 0;
int maxRow = 0;
maxRow = ds.Tables["tblImpi"].Rows.Count;
ds.Tables["tblImpi"].Rows[inc].Delete();
Update();
maxRow = ds.Tables["tblImpi"].Rows.Count;
inc--;
// NavigateRecords();
MessageBox.Show("Record Deleted");
This one works but only delete records from the first row only
Ramesh MaruthiRank #937 · 2.2K reputationPosted Oct 2, 2014, 5:53 AM EDT
Look at this link
http://www.dotnetcurry.com/showarticle.aspx?ID=132
kevin22 reputationPosted Oct 6, 2014, 5:32 AM EDT
H Ramesh I went to the link that you recommend I must check and I used the following code
and I still the an exception error
private void btnDel_Click(object sender, EventArgs e)
{
if (!e.Row.IsNewRow)
{
DialogResult res = MessageBox.Show("Are you sure you want to delete this row?", "Delete confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res == DialogResult.No)
e.Cancel = true;
}
}
Ramesh MaruthiRank #937 · 2.2K reputationPosted Oct 6, 2014, 7:46 AM EDT
Are you selecting some rows or not from a gridview while deleting ?
kevin22 reputationPosted Oct 6, 2014, 10:38 PM EDT
Yes I'm selecting a row to delete but nothing is happening,I'm struggling to understand this part
of code,
private void dgv_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
(object sender, DataGridViewRowCancelEventArgs e) I'm not sure whether is a
button click event or where to look for it in the properties,because I tried to
a button,letter I removed that button click
Munesh SharmaRank #174 · 13.9K reputationPosted Oct 6, 2014, 11:11 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Oct 7, 2014, 6:10 AM EDT
this links updates even in the database :
http://www.codeproject.com/Questions/454755/Csharp-Datagridview-update-and-delete
if you just want to delete on the front end you can do in this way