Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a value of selected row or cell or anything from asp.net 4.0 gridview 1

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
At some point I need the record ID from the row I select to do something with it.
I have scoured the internet, found tons of examples which no longer work in net 4.0. or options that evidently were in 2008 or 2006 or ??? but don't work anymore, don't have the same arguments are not the right data type? etc etc.
It might have something to do with datakeys although I have not found anything that works.
I want the Student_ID which is in column 2 of a particular row I click the command button in that row.

This code works in VB.NET 2010
Code:
   Private Sub DataGridView1_RowEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter

          Me.txtStudentID.Text = Me.DataGridView1.Rows(e.RowIndex).Cells("Student_ID").Value
        Me.txtLastname.Text = Me.DataGridView1.Rows(e.RowIndex).Cells("Lastname").Value
but the grid view in ASP.NET 4.0 does not support e.RowIndex or e.row.rowindex ??? which it used to.
Does anyone know how to do the same thing ASP.NET 4.0 VB code?

TIA

DougP
 
This is one of the things that bit me when I started.
You're trying to treat the Web based GridView like a Windows based DataGridView. This is why you had to fully declare the event type.

You're not going to that level of control over what the user is doing.

Lodlaiden

You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)
 
this is using Visual WEB Dev 2010 Express which is asp.net 4.0.
There has to be a way to get a value from a cell in a datagrid when a button is click in that row which is in an extra added column. But all I can manage to get is the row id of the row clicked using a select button added into that extra column. The grid is connected to a SqlDataSource filled by a stored procedure.
If I have this sample grid... I want to click the 'Pick me' button on row 2 and somehow get the word 'Sam' from the 'name' column, or '33' from the 'ID' column into a label control.
--------------------
RowID Name ID SelectButton
1 Fred 23 Pick me
2 Sam 33 Pick me
3 Sally 45 Pick me
------------------------
here is all I have so far when I click the 'pick me' button on RowID 2 it fires 'SelectedIndexChanged' event and lblRowNumber has a 2 in it.
I want me.lbl_name to have the word Sam and me.lbl_ID to have 33
Code:
   Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

        Dim RowID As Integer = GridView1.SelectedRow.RowIndex + 1
        Me.lblRowNumber.Text = RowID.ToString
        [COLOR=red]lbl_ID = GridView1.????.Row_I_Selected.fields("ID") '<<< what do I need 
here[/color] 

 End Sub

After scouring the WEB...
It appears that whatever code used to work in 2005, 2008 or previous, does not work in 2010.

TIA



DougP
 
You were never able to access values in a grid by column name, only by index. Perhaps you can do it in a Windows app, but not a web app.
What you need to do is us the RowCommand event of the grid. You assign a "CommandName" and "CommandArgument" to your button. then in the Rowcommand event of the grid you can get your values. There are many examples on line.
 
jbenson001, I wrote in a VB 2010 already and now want it work on the WEB?

I want it to work on all the phones and tablets and whatever else can surf, "mobily".

I wrote it classic ASP and VB.NET and now want to use ASP.NET? or it should be called DOT "NOT"?
or DOT WAS. since it used to work or so scads of posts with dates 3-5 years ago proclaim.
You just gotta laught right? I mean come on, 6 versions of .NET and we can't get a value from a grid? Anymore?

DougP
 
DougP,

I write web based .net code all day. You're frustration comes from a lack of understanding the differences between classic and .Net, which are significant.
It's like german and enlish. They're both latin based languages so they use similar structures, but you can't just throw a few umlats in there and bamo! Gemanese.

There are intricacies making an application work across multiple browsers and platforms, which .Net enables, but does not provide for free.

Back to your post,
You do have access to the values in the grid. You have to access them in the proper fashion. Normally this is done with something like:
//psuedo code
gvMyGrid.Rows[gvMyGrid.SelectedIndex].Cell[2].Value.ToString()


Lodlaiden



You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)
 
Well there you go Qik3Coder, have a star! now that's what I'm talking about.

this is what actually works
changed the word "cell" to "cells" and there is no ".value" parameter but ".text" works. and since its a string we don't need ".ToString".

Code:
 Me.lblRowNumber.Text = GridView1.Rows(GridView1.SelectedIndex).Cells(0).Text

And Whala I get "Sally" in lblRowNumber, just testing it.

DougP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top