473,378 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1780

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark Jones | last post by:
I'm trying to delete a row from a DataGrid using a ButtonColumn with CommandName="Delete" by deleting the row in the underlying DataTable datasource, but it doesn't seem to be working at the...
3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. I put a datagrid in my inherited user control,...
3
by: vcornjamb | last post by:
Hello, I am developing a web form that contains some buttons and a data grid which has as its last column link buttons that will delete the data associated with that row. Everything works fine,...
2
by: Jim McGivney | last post by:
I am using a Microsoft Book as a guide and trying to construct a Delete Command Event Handler for a DataGrid. The book lists the statement: ...
5
by: Agnes | last post by:
I an addnew and modify all the rows in my Datagrid . However , If i highlight the whole rows , press 'delete' and the process daInv.update(dsInv,"mytable) I got the following error "This row has...
1
by: johnb41 | last post by:
I have a datagrid that successfully allows editing and updating (with dataadapter.update command). I want the user to be able to edit the "text" in the datagrid, but i DON'T want the user to be...
4
by: Henry J. | last post by:
Can somebody tell me how to delete multiple rows in a datagridview selected by the user? I tried the following two methods to no avail. I have a myDataGridView that is bound to myDataTable's...
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
10
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.