kaylendarr
kaylendarr

Reputation: 41

VB/ASP.NET, Access GridView cell value to variable

Here's the app:

Have a grid view with some rows. When the user clicks edit on the web form it opens up a couple of the fields for editing. What I'm trying to do is when the RowUpdating fires, is check the value of one of the fields in the grid view and verifies its within a certain range and then cancels if its outside of that range.

I actually have almost all of that framework done, except I can't obtain the value from the field in the GridView.

I've searched this site up and down with a few others, and all list some solutions but none have worked. I get cannot be converted to string/integer, plus some others.

Any suggestions? Here's my lastest:

    Private Sub gvCourses_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCourses.RowUpdating
    Dim x As String
    x = gvCourses.SelectedRow.Cells[3].Text
    MsgBox(x) ' Just a check to see if I get the variable to display the correct value, not part of the final app

I've also tried different variations of the above, none of which worked. The one above I get System.Web.UI.WebControls.TableCellCollection cannot be converted to String.

I've seen several examples but none of them work. Anyone have any suggestions?

Thanks.

Upvotes: 1

Views: 7895

Answers (3)

kaylendarr
kaylendarr

Reputation: 41

So I finally figured out how to accomplish what I needed. For anyone else that comes accross this, this may be helpful to you.

The original issue: - "What I'm trying to do is when the RowUpdating fires, is check the value of one of the fields in the grid view and verifies its within a certain range and then cancels if its outside of that range. "

The Solution:

    Private Sub gvCourses_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCourses.RowUpdating
    Dim x As Integer = e.NewValues.Item("Column_Name")
        ' The above grabs the value from the text box of the specified column and assigns it to x. I can then use x to do the validation I noted above.

Appears I was having a logic issue and trying to use SelectedRows etc, when I was completely off base for what I was trying to do.

Special thanks to scartag for your several attempts to help and to Dennis. Thank you both!

Upvotes: 0

scartag
scartag

Reputation: 17680

Trying changing that line to.

x = TryCast(gvCourses.SelectedRow.Cells(3).Controls(1), TextBox).Text

When the gridview is in update mode it renders a textbox (if it is bound to textual data) and the cell adds it to its controlcollection.

Upvotes: 2

Dennis Rongo
Dennis Rongo

Reputation: 4611

Have you tried the following?:

Dim x As String
x = gvCourses.Rows[e.RowIndex].Cells[3].Text;

Upvotes: 1

Related Questions