Solved

Assigning a datagrid row item to a label

Posted on 2007-11-30
8
829 Views
Last Modified: 2012-08-13
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:

System.Web.UI.WebControls.GridViewRow

Code:

      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

0
Comment
Question by:barnesco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 20385859
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
0
 

Author Comment

by:barnesco
ID: 20386011
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
0
 
LVL 16

Assisted Solution

by:anoyes
anoyes earned 200 total points
ID: 20386770
Sorry, my bad.

lblStore.Text = grdPromo.Rows(grdPromo.SelectedIndex).Cells(2).Text
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 2

Accepted Solution

by:
yossi_intlock earned 300 total points
ID: 20386818
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">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CFTITLE") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("CFTITLE") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
use
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
DataTable1.select("key='keyValue'") as datarow
0
 

Author Comment

by:barnesco
ID: 20386870
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.
0
 
LVL 10

Expert Comment

by:digitalZo
ID: 20387108
How are you selecting the row? Have you given any checkbox or select button or something to select the row?

Check out this:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanged.aspx

The SelectedIndexChanged event is raised when a row's Select button is clicked, but after the GridView control handles the select operation
0
 

Author Comment

by:barnesco
ID: 20388131
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" ... />

and

      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?
0
 

Author Comment

by:barnesco
ID: 20388693
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET Content Page 3 64
ASP.net Markup for Sub Menu 2 65
Left aligned $ and right aligned amount ASP.NET 7 45
dropdownlist in asp.net vb. 3 23
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question