Introduction Normally we use DataGrid to show our records in the web page. & uses CheckBoxes to select one or more records to delete from the DataGrid. If we want to delete any single row then there is no tough work. But Problems comes when we want to delete multiple records simultaniously by selecting rows with the help of CheckBoxes. Means, If the user Checked one or more CheckBoxes then all coresponding rows should be deleted. Lets see how:
How to select multiple records from DataGrid First of all we have to identify the rows to be deleted. Suppose in our aspx page we have a DataGrid named dgSkills and it contains a template column with CheckBoxes. Now user clicked in the required fields.Now Our first job is to retrieve that rows. For this purpose your table must contain a PK field or Unique field to identify the rows uniquely.Lets see how:
Step 1: Find the Checked rows Step 2: Concatinate with "," operator Step 3: Perform Delete operation
Identifying selected rows
Declare a string variable that will store the PK ID of all selected rows
String strStateID = String.Empty; string strSkillID foreach(DataGridItem dgItem in dgSkill.Items) if (((CheckBox)dgItem.FindControl("chkSelect")).Checked == true) strSkillID += "," + dgItem.Cells[1].Text.ToString(); if(strSkillID != string.Empty) strSkillID = strSkillID.Substring(1);
The last line remove the first "," from the string. Now you have all the row like (2,5,7,10) and you have to perform delete operation. I am using upadate in place of Delete and place two extra Columns in my table :
Alter Table SkillMaster ADD Active bit Default 1,DateOfDeletion dateTime
When I want to delete any record, I will set Active to "0" and DateOfDeletion to Current date. It is the good practice to keep record in your database. Because when you want to show all active users then you can do it easily by "where active = 0" clause and in future if you want to see any past record or deleted record then you can see easily because you have all records in your database. Most of the Companies use this type of strategy.
Continue........
Summary In this article we saw that how to identify the selected rows & retrive them and how we can save all those rows on which we have to perform DELETE Operation or Update Operation.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|