This is a migrated thread and some comments may be shown as answers.

Get selected item column value in code behind

2 Answers 384 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 24 Feb 2009, 05:10 PM
Hi

I am trying to use the 'select' button to add a RadGrid item to a sort of 'cart' and to
confirm the addition by writing label text. This is the code:
Protected Sub RadGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.SelectedIndexChanged  
 
        LabelCart.Text = RadGrid1.SelectedValue("Name").Text + " was added to your request list."  
 
End Sub
If I leave out the  RadGrid1.SelectedValue("Name").Text +  part it displays the text when an item is selected.
However with the code as above, it generates an error:

System.NullReferenceException: Object variable or With block variable not set 

I have spent many unhappy hours with 'null reference exception's in the past ... can you put your
 finger on it right away?

Or how should I be doing this?

Thanks a lot!

Clive

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Feb 2009, 04:21 AM
Hi Clive,

Try the following code snippet and see whether it helps. SelectedValue returns the DataKeyValue for the selected row in the RadGrid control.

VB:
 
 
     Protected Sub RadGrid1_SelectedIndexChanged(ByVal sender As ObjectByVal e As EventArgs) 
         'to get the key value for the selected row 
         LabelCart.Text = RadGrid1.SelectedValue.ToString() & " was added to your request list." 
         
         'to get the cell value of the selected row 
         For Each item As GridDataItem In RadGrid1.SelectedItems 
             LabelCart.Text = item("Name").Text 
         Next 
     End Sub 
 
 

Shinu



0
Clive Hoggar
Top achievements
Rank 1
answered on 25 Feb 2009, 03:11 PM
Thanks!
 
That worked a treat. (After converting to VB!)

Clive
Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Clive Hoggar
Top achievements
Rank 1
Share this question
or