By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,013 Members | 1,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,013 IT Pros & Developers. It's quick & easy.

Delete Rows From DataGrid

P: n/a
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
FillDataGrid()
End If
End Sub

Sub DeleteRows(obj As Object, ea As EventArgs)
FillDataGrid("Delete")
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()
Next
End If

dg1.DataSource = dSet
dg1.DataBind()
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?

Dec 4 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.