How to get values from selected item in a gridview

J

Julia B

Hi all, just as I'd got the hang of datagrids in 1.1 they change to gridviews
and everything is done differentley...... I'm really stuck so help would be
appreciated!

I've got a gridview which has a select command. When the user clicks the
select command I want the data from the selected row to be pasted into some
controls/fields I have, the problem is that I can't seem to be able to get
the data, here's one of the variations i've tried:

Private Sub dgDepts_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgDepts.RowCommand
If e.CommandName.Equals("Select") Then
'populate the department edit fields with the data from the
selected record
Me.tbDept.Text = Me.dgDepts.SelectedRow.Cells(0).Text
End If
End Sub

I get an error message at Me.dgDepts.SelectedRow.Cells(0).Text as it's
showing as nothing.

I've also tried me.dgDepts.sSlectedDataKey.Item(0).ToString but get the same
message (although I'm not sure I really understand this new datakey lark).

I've searched high and low to get examples of how to do this and have found
lots but nothing that matches what I need to do.

Any clues greatly appreciated.
Julia
 
Joined
Oct 9, 2008
Messages
2
Reaction score
0
Try This ....

Try this code instead:

Protected Sub dgDepts_selectedindexchanged(ByVal sender as object, byval e as sytem.eventArgs) handles dgDepts.selectedindexchanged

dim xRow as gridviewrow = dgDepts.selectedRow
tbdept.text = dgDepts.datakeys(XRow.Rowindex).item(0) ' this corresponds to your first datakey item

End Sub

A. Make sure that you have set up your datakey for the gridview (DataKeyNames property of the gridview) first to the required field (in this case something related to dept or deptname).
B. And while we're on the subject, make sure the gridview is first bound to a datasource! Setting up the datakeynames is easy with the built in 'wizard'
C. The fastest way to set the above code is to double click on the dgDepts gridview. Visual Studio will generate for you the necessary prerequisite selectedindexchanged (i.e Protected Sub ..... ) headers. Just fill in the code above.

Some historical perspective. When you were using datagrid in asp.net 1.1, one of the common practises was to populate the datagrid columns with the necessary fields ( such as ID, person name, department name and so forth). Then you would probably hide the column from the user view - i.e turn it invisible. Programmatically, when the user then selects the row, you would simply reference the hidden column to get the information out onto a control somewhere such as a text box etc, like you did in your code.

When Gridviews were first introduced in asp.net 2 beta, you could typically still do the above. However when asp.net 2.0 was finalised, the ability to reference an invisible column was removed. Instead, the use of datakeys were recommended as being more secure.

I hope that helps Julia.

Just hang on in there. Transitioning to asp.net 2.0 and above is fun. There are lots of shortcuts in the code. But I have to admit, it is rather a pain to keep track of!

Regards,
Bandi.
 

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

Top