protected void gvCurdOperations_RowDeleting(object sender, GridViewDeleteEventArgs e){int empId= int.Parse(gvCurdOperations.Rows[e.RowIndex].Cells[1].Text);SqlConnection con = new SqlConnection("YourConnectionStringDetails");con.Open();string st = "delete from [EmployeeDetails] where EmployeeId=" + empId;sqlcommand cmd = new SqlCommand(st, con);cmd.ExecuteNonQuery();con.Close();BindDataToGrid();//This method is for reloading the grid view data again...}
public void BindDataToGrid(){SqlConnection con = new SqlConnection("YourConnectionStringDetails");SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM [EmployeeDetails]", con);DataSet ds = new DataSet();ad.Fill(ds, "Categories");gvCurdOperations.DataSource = ds;gvCurdOperations.DataBind();}
using System.Data;using System.Data.OleDb;namespace DataGridViewOperation{ public partial class Form1 : Form { int rindex; OleDbConnection con = new OleDbConnection("Connection String"); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter da = new OleDbDataAdapter(); DataTable dt = new DataTable(); Boolean bind; public string colindex; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { LoadGrid(); } void LoadGrid() { con.Open(); cmd = new OleDbCommand("select * from emp", con); da = new OleDbDataAdapter(cmd); da.Fill(dt); bind = true; dataGridView1.DataSource = dt; con.Close(); //I have set Primary key column value in the DataGridview as Readonly because based on that value we can update record dataGridView1.Columns[0].ReadOnly = true; bind = false; } private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) { //store current row index in the variable rindex = e.RowIndex; } //delete using below code for selected row private void button1_Click(object sender, EventArgs e) { con.Open(); string query; query = "delete from emp where eno='" + dataGridView1.Rows[rindex].Cells[0].Value + "'"; cmd = new OleDbCommand(query, con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Information update Successfully", "Customer Information"); } }}
protected void btnDeleteRows_Click(object sender, EventArgs e){ foreach (GridViewRow row in GridView1.Rows) { CheckBox checkbox = (CheckBox)row.FindControl("chkSelect"); if (checkbox.Checked) { int employeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "DELETE FROM EmployeesCopy WHERE Id="+employeeID ; cmd.Connection = con; con.Open(); int numberDeleted = cmd.ExecuteNonQuery(); Lable1.Text = numberDeleted.ToString() + " employees were deleted."; con.Close(); } } }