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
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?

Dec 4 '06 #1
0 1780

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

Similar topics

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...
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,...
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,...
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: ...
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...
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...
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...
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...
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...
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...
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...
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...
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,...
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...
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...
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
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...
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.