Remove row from datagrid dynamicly

M

moondaddy

I have a datagrid where a user can edit a number in one of the columns for a
quantity value. On the postback I loop through all of the datagrid items
and if the quantity has changed, then I update the value of the extended
price cell for that datagrid item.

Also, the datagrid has a checkbox in each row called "Remove" where if the
checkbox is checked I delete that row from the underlying datasource and set
a flag which tells me to rebind the datagrid before the page sent back to
the client. Rather that raising a flag and rebinding the datagrid, I was
hoping I could simply remove that datagrid item (row) as I'm looping through
the items collection. Is this possible? (Something like 'item.delete'...)

Thanks.
 
G

Guest

moondaddy -
What I would recommend is that you use the events built into the grid control to handle the functions that you perform. I use the OnEditCommand OnDeleteCommand and the OnUpdateCommand in either a datalist or grid to handle the functions that you explain below. Below is an example of the OnDeleteCommand that would solve your checkbox problem below (this is from a datalist - but works with datagrid, too):

Sub listImages_DeleteCommand(ByVal s As Object, ByVal e As DataListCommandEventArgs)
Dim iImageID As String = listImages.DataKeys(e.Item.ItemIndex)
Dim ImageName As String
Dim BaseImageFileLoc = ConfigurationSettings.AppSettings("BaseFileLoc") & "images\"
Dim findDivisionName As New globalFunctions
Dim sDivisionName As String
With findDivisionName
.GalleryID = ddlGalleryID.SelectedItem.Value
sDivisionName = .findDivisionName
End With

sSQL = "select imagename from images where imageid = " & iImageID
oConn.Open()
Dim cmd1 As New SqlCommand(sSQL, oConn)
Dim dr As SqlDataReader = cmd1.ExecuteReader

Do While dr.Read
ImageName = dr.Item("imagename")
Loop
dr.Close()
oConn.Close()

'Response.Write(BaseImageFileLoc & sDivisionName & "\" & ImageName)

File.Delete(BaseImageFileLoc & sDivisionName & "\" & ImageName)

Dim deleteImage As New imagesDB
With deleteImage
.oConn = oConn
.ImageID = iImageID
.deleteImage()
End With
listImages.EditItemIndex = -1
BindDataList()
End Sub

There are a lot of extra pieces of this Sub - but the important parts are the e.Items collection - that is the acutal row in the grid that you are working on. Let me know if this helps.
 
S

Steven Cheng[MSFT]

Hi Moondaddy,

In addition to rlthurston's suggestion on use the DataGrid's buildin
DeleteCommand event, here are some of my suggestions:

Since the Webform DataGrid control (as well as DataList or Repeater) is
Template databinding control, it's items(displayed records) are all
generated from the binded datasource and then stored in viewstate. We can
set some certain rows(in the DataGrid's Items collection)'s Visible to
false to hide them but can't remove them this way. And if we want to
represent the changes in the DataSource, the formal and recommended
approach is just rebind the grid with updated datasource. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
M

moondaddy

Thanks for the above 2 postings. Since I can't actually remove or delete
the row, I'll simply rebind the grid. On each postback I loop through the
grid to capture any changes and then update the database. If I leave the
rows there and make them not visible, then I'll just have to do extra
processing and logic to account for that.

Thanks again.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top