delete a seleted row from a DataGrid using c# windows application


Double click the dataGrid and use the below code

private void DataGridvie_selected(Object sender,EventArgs e)
DataSet da=new DataSet();
//when you select a row that index will storing in i variable
int i=datagrid1.selectedIndex;

int empidval=ds.Tables["emp"].Rows[i][0].ToString();
//above what you had selected that row value exa:empid has 101 is stored in empidval vairable

//write the delete query

SqlConnection conn=new Sqlconnection("conn string");
SqlCommand cmd=new SqlCommand("delect emp where empid="+empid+"",conn);
MessageBox.Show("Row Deleted");


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: