Delete Rows From DataGrid



All the rows in a DataGrid are accompanied by a CheckBox. When a user
checks the rows & clicks a Button, the checked rows get deleted. For
e.g. assume that the DataGrid displays 10 rows. A user checks the rows
2, 4, 6, 8 & 10. When he clicks the Button, the page posts & the next
page displays only 5 rows i.e. row 1, 3, 5, 7 & 9. The checked rows are
populated in a ViewState variable & will have values like 2, 4, 6, 8,
10, (note that there is a space after the last comma): The Button
exists outside the DataGrid. This is how I am implementing this (the
ViewState variable is populated when a CheckBox is checked - the code
of which I haven't reproduced here)

Sub Page_Load(......)
If Not (Page.IsPostBack) Then
End If
End Sub

Sub DeleteRows(obj As Object, ea As EventArgs)
End Sub

Sub FillDataGrid(Optional ByVal Param As String = "")
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(".....")
sqlDapter = New SqlDataAdapter("SELECT * FROM MyTable WHERE ID <=
10", sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Table1")

If (Param = "Delete") Then
Dim arrItems As Array
Dim strItem As String
Dim strViewState As String

strViewState = Left(ViewState("SelRows"),
Len(ViewState("SelRows")) - 2)
arrItems = Split(strViewState, ", ")
For Each strItem In arrItems
dSet.Tables(0).Rows(CInt(strItem) - 1).Delete()
End If

dg1.DataSource = dSet
End Sub

The above code does delete the checked rows from the DataGrid when the
Button. In this case, the rows 2, 4, 6, 8 & 10 get deleted & the
DataGrid display rows 1, 3, 5, 7 & 9. Next the user checks the rows 1,
3 & 7 & clicks the Button.

After the page posts, the rows 1, 3 & 7 get deleted from the DataGrid
but the 5 rows (2, 4, 6, 8 & 10) which were deleted earlier again get
displayed in the DataGrid i.e. the DataGrid displays the rows 2, 4, 5,
6, 8, 9 & 10 whereas I want that under such circumstances, the DataGrid
should display only the rows 5 & 9.

How do I accomplish this?


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DataGrid Edit Problem 9
DataRow Delete Method 5
DataGrid.Columns(Index) 3
Deleting multiple rows in Datagrid 0
Deleting a row in a datagrid 2
DropDownList DataGrid 1
Controls 3
Text Alignment In DataGrid? 7