Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid/GridView - Part 2: Maintaining CheckBox State Acr

Repopulate our DataGrid

This is all brought about in a more or less similar manner as storing the values. The RePopulateCheckBoxes()method listed below starts of by setting up an ArrayList based on the values stored in Session State. Next, as before, it loops through the current page DataGrids items, and if Session State contains any matching values that happen to be on the current page, it'll then proceed and set the appropriate checkbox to true.

Sub RePopulateCheckBoxes ()

CheckedItems = New ArrayList

CheckedItems = Session ("CheckedItems")

If Not IsNothing(CheckedItems) Then

'Loop through DataGrid Items
For Each dgItem in MyDataGrid.Items

ChkBxIndex = MyDataGrid.DataKeys(dgItem.ItemIndex)

'Repopulate DataGrid with items found in Session
If CheckedItems.Contains(ChkBxIndex) Then

CheckBox = CType(dgItem.FindControl("DeleteThis"), CheckBox)
CheckBox.Checked = True

End If

Next

End If

'Copy ArrayList to a new array
Results = CheckedItems.ToArray(GetType(String))

'Concatenate ArrayList with comma to properly send for deletion
deletedIds = String.Join(",", Results)

End Sub

Additionally, the aforesaid method at the end also copies the ArrayList into a new array from which the values get cleanly concatenated with commas, so they can be batch deleted.

The rest of our code is pretty undemanding. The remaining functions are all self explanatory. For one, you have the SortOrder() function, which properly sorts the order based on the values passed to it, in turn sorting the DataGrid accordingly. The other notable function is the DeleteAllIds() method that deletes all the values that were setup in the method listed above. Moreover, since one might uncheck any checkbox on the given current page, then proceed to delete their selections, we need to support this action by including the GetCheckBoxValues() and RePopulateCheckBoxes() methods, whereby it'll regrab the values and update Session State accordingly, so the final delete reflect this change.

Finally, you have the ClearDataGrid() method, that willclear all Session State values and rebinds our DataGrid from the top. And that all she wrote.

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous