Assigning a datagrid row item to a label

I'd like to assign a data item on a grid row to a label when it's selected. I used the following, but I receive the error:



      Protected Sub grdPromo_SelectedIndexChange(ByVal sendar As Object, ByVal e As System.EventArgs)
            lblStore.Text = grdPromo.Rows.Item(2).ToString
      End Sub

Any suggestions? Thanks.

Open in new window

Who is Participating?
yossi_intlockConnect With a Mentor Commented:
to get a specific value in a row use: GridView1.Rows(GridView1.SelectedIndex).Cells(2).Text
the cell index is your datafield index. if you are using templated fields than youll have to fnd you control for example if you have a field like that  :
<asp:TemplateField HeaderText="CFTITLE" SortExpression="CFTITLE">
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CFTITLE") %>'></asp:TextBox>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("CFTITLE") %>'></asp:Label>
Dim lbl As Label = GridView1.Rows(GridView1.SelectedIndex).FindControl("Label1")
        Dim myText As String = lbl.Text

to get your value directly from your datasource - add a datakeyfield to ur gridview than get this key by the selected row index
Dim key As DataKey = GridView1.DataKeys(GridView1.SelectedIndex)
        Dim keyValue As String = key.Value
and than find your datarow (in the datasource) by this key"key='keyValue'") as datarow
You haven't specified which row to get the item from.  For example, to reference the third column in the second row, use lblStore.Text = grdPromo.Rows(1).Item(2).ToString

Or to base the row off the selection of the gridview, use lblStore.Text = grdPromo.Rows(grdPromo.SelectedIndex).Item(2).ToString
barnescoAuthor Commented:
The second is more for what I need,

lblStore.Text = grdPromo.Rows(grdPromo.SelectedIndex).Item(2).ToString

but I'm receiving the error: 'Item' is not a member of System.Web.UI.WebControls.GridViewControls
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

anoyesConnect With a Mentor Commented:
Sorry, my bad.

lblStore.Text = grdPromo.Rows(grdPromo.SelectedIndex).Cells(2).Text
barnescoAuthor Commented:
It's still not returning a value. The index seems right, returning the correct row, and I've tried all the cell values possible, but no values are being returned.
How are you selecting the row? Have you given any checkbox or select button or something to select the row?

Check out this:

The SelectedIndexChanged event is raised when a row's Select button is clicked, but after the GridView control handles the select operation
barnescoAuthor Commented:
That's exactly the site I looked up last night, and there is a select row in the grid. In fact, it calls another grid to display a detailed view (I just need a label to display the view).  I have

<asp:GridView ID="grdPromo" OnSelectedIndexChanged="grdPromo_SelectedIndexChange" ... />


      Protected Sub grdPromo_SelectedIndexChange(ByVal sendar As Object, ByVal e As EventArgs)
            Dim row As GridViewRow = grdPromo.SelectedRow
            lblStore.Text = row.Cells(2).Text         'grdPromo.Rows(grdPromo.SelectedIndex).Cells(2).Text
      End Sub

(I commented out what was suggested before). I've tried different cell positions, nevertheless, 2 should be right, being the third cell). When I perform a debug, the count shows correctly (7), but item shows "Item = In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user."

Could that be something?
barnescoAuthor Commented:
I apologize. I'm using DotNetNuke and it must have something to do with how it returns data--I just tried the above code in another environment, and it works great.

Thank you, all, for your contributions. They were very helpful.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.