471,595 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

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
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
0 1673

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jim McGivney | last post: by
5 posts views Thread by rn5a | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.