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

Posted by Syedshakeer under Windows Forms category on | Views : 14721
Place a DataGrid on the form and populate it, now 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 j = datagrid1.selectedIndex;

int empidval = ds.Tables["emp"].Rows[j][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");


Comments or Responses

Posted by: Sudheep.grandhe on: 12/17/2011 Level:Starter | Status: [Member] | Points: 10

i want to Delete a seleted row from a DataGrid using c# windows application.for that i have two tables tbl_report1,tbl_report2

tbl_report1 columns:invoiceno(FK),particulars,quantity,perprice,amount,compname,invoicedate,ondate,orderno,invoiceid.

tbl_report2 columns:compname,compaddress,invoiceno(PK),orderno,ondate,invoicedate,amountstatus,netamount,netvat,tamount

from above two tables i have to design a query for delete and update.based on invoiceno.please do the needful.

Login to post response