Watch Video
In this article I am going to explain how to delete row in Gridview using C# code.
Drag and drop GRiview controller to form and using data source bind data to Gridview.
Click on the Gridview and you can see arrow top right. When you click on arrow pop menu will display named as Gridview Tasks.
In that Gridview Task menu, you have to check the Enable Selection, and click on the Edit column. Next change the text on SelectedText as Delete. Then you can see the first column as Delete.
Next click on Gridview and go to properties and click Even icon, then you can see all events of Gridview.
From that event list go to SelectIndexChanges and double click. Then SelectIndexChanged event coding window will display and you can write the code as below
{
GridViewRow row = GridView1.SelectedRow;
txtBcode.Text = row.Cells[1].Text;
txtBname.Text = row.Cells[2].Text;
SqlConnection sqlConnection1 = new SqlConnection("Data Source=SERVER;Initial Catalog=SUPERMARKET;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "DELETE Banks WHERE b_code =" + int.Parse(txtBcode.Text) + "";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
GridView1.DataBind();
}
Creating the SQL connection string as below
SqlConnection sqlConnection1 = new SqlConnection("Data Source=SERVER;Initial Catalog=SUPERMARKET;Integrated Security=True");
After getting the SQL connection string and Open the SQL connection. As the next step execute the SQL Command for Deleting Record from SQL Table, and Bind the data source again to Gridview.
Actually Gridview row not deleteing from Gridview. Record deleting from SQL Database Table record and SQL Database Table again bind with Gridview using Data source.
Run the project and you can see data with Gridview with first column as Delete. when you click on Delete link button. Selected row will delete.
Watch Video
0 comments:
Post a Comment