kevin

kevin

  • NA
  • 23
  • 16.2k

Delete a record from a datagridview in Winform

Oct 2 2014 9:05 AM

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");

 
 
 
I get this exception error with the above code, Object reference not set to an instance of an object. 
 
 
Then I tried using the following code.
 

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 

Answers (6)