How can I delete a row from excel sheet based on product id provided in textbox1. Using that product_id it should delete the whole row from excel sheet & display updated data in datagridview?
Asked
Active
Viewed 3,878 times
-1
-
http://stackoverflow.com/questions/9951188/deleting-rows-from-an-excel-file-using-c-sharp – Karthik Jul 14 '13 at 05:35
2 Answers
1
if you'll look at here you'll find explanation on how to delete using Range.Delete
Method. this method is for deleting from exel and as Karthik posted you can do
((Range)worksheet.Rows[i]).Delete(shiftDirection)
for your code in order do delete data from row i.
the shiftDirection
is optional and Specifies how to shift cells to replace deleted cells.

No Idea For Name
- 11,411
- 10
- 42
- 70
0
private void deleteButton_Click(object sender, EventArgs e)
{
OleDbConnection myConn = new OleDbConnection(connectionString);
string excelDelete = "DELETE FROM [Sheet1$] WHERE ID = '" + dgvExcelList["ID", dgvExcelList.CurrentRow.Index].Value.ToString() + "'";//modify as your need
OleDbCommand Deletecmd = new OleDbCommand(excelDelete, myConn);
myConn.Open();
Deletecmd.ExecuteNonQuery();
myConn.Close();
/*code to update datagridview*/
}

roanjain
- 1,252
- 4
- 14
- 32