asp.net more gridview help needed

Soldato
Joined
1 Feb 2006
Posts
8,188
I am using the following code to be executed when a checkbox is ticked within my gridview control.

Code:
Protected Sub cbPreviouslyViewedCheckbox_Changed(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvImages.SelectedIndexChanged

        ' Get the Checkbox on the selected row.
        Dim checkbox As CheckBox = DirectCast(sender, CheckBox)

        ' Get the Surname for the selected row
        Dim Image_ID As Integer = WHAT GOES HERE

        lblMessage.Text = Image_ID.....

I need to be able to get the value of another cell in the same gridview row as the checkbox that was checked. How can I go about getting this value?

Basically the scenario is this:

There can be multiple checkboxes selected at any time - these are populated at page load. A user clicks another checkbox (the above function executes) and I want to execute code to update the database. To do this I need to get the value of an IMAGE_ID field which is in the same row of the Gridview that the checkbox was in. I cannot use a for each statement as I am only dealing with this given row. Any ideas how I can get cell(1).Text for this row only?

Thanks
 
Last edited:
you can do something like this, if the selected row event has been triggered.

Code:
gridview.SelectedRow.Cells(1).Text

Be careful there's some issues if it's hidden data. If you can't get it you need to set to visible in the property settings. Then in the gridview row created event manually set GridViewRowEventArgs e.row for each cell visibilty property to false. If that makes sense?
 
Last edited:
Hi, thanks for the reply.. i tried the above and it gives a 'object reference not set to instance of an object' warning. If I was iterating through each row it would be easy as you would do something like row.Cells(1).Text but this way is different. The problem is that some textboxes will be ticked on page load (comes from database bit value) but what i really need is that when an unticked checkbox is ticked and the checked_changed event occurs then i need to be able to uniquely identify which gridview row it was called from.

Dim checkbox As CheckBox = DirectCast(sender, CheckBox) will obviously uniquely identify the checkbox but how can i do the same for the cell value 1 of the same row? I'm totally new to asp.net so strugglin with it a lot!
 
Ok, I'm trying on my vb.net head at mo. So I may get the syntax wrong as I'm converting direct from my C# code as I have something similar.


Code:
Dim Image_ID As Integer = Convert.ToInt32(DirectCast(sender, GridView).SelectedRow.Cells(1).Text)

Right, I'm doing this as your event uses

Handles gvImages.SelectedIndexChanged​

Which is the gridview event so the sender object should be a gridview object. breakpoint and check that with a watch or something. Then check the Selected row to see if it's has a value. If so then check the cells count. to see how many actual cells you have. I warn you now messing around with grids can get very dirty. I know
 
just got it working using this...

Code:
Dim gvr As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)

Dim Image_ID As Integer = gvr.Cells(1).Text

Thanks for the replies anyhow
 
Back
Top Bottom