Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Determine value of selected row in webform datagrid
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00868119
Message ID:
00868489
Views:
31
Thanks Rick

Your example pointed me in the right direction, here are the ways I have been exploring subsequently.

I will post my rough working experiment here to help anyone trying to do it in VB
I hope its of some use to someone, because I spent a confusing few hours trying to figure it all out.

It was infuriating to have to go through all this hassle when in VFP it would be a trivial matter. Sigh....still now I can write for the web.

Folks please feel free to add addressing methods I have missed and correct mistakes I have made.
  Private Sub GridPurposes_ItemCommand(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles GridPurposes.ItemCommand
        ' Your grid has a property called datakey, this must be populated with the data 
        ' you are trying to retrieve after a button in the grid has been pressed.

        ' An id needs to be added to the grid button before you can address it
        ' You can add a button id by right clicking on the grid
        ' and choosing Edit Template. Below we use the id lbMark
        ' Once you have an id for the button you can use
        ' something similar to below to return the text thats stored on the control

        ' You can use findcontrol to find the individual button item by its id
        ' example 
        ' Dim pd As LinkButton = e.Item.FindControl("lbMark")
        ' Dim strX As String = pd.Text
        ' or
        ' You can use e.CommandSource which will return a reference to the button that was clicked
        ' Dim btn As Object = e.CommandSource
        ' btn.text will contain the text of the button pushed
        ' or
        ' If the cell you want to refer to does not contain a object
        ' you can refer to it using the cells property
        ' Dim strX As String = e.Item.Cells(0).Text
        or
        ' If the cell does contain an object and you need to refer to it elsewhere in your prog
        ' Using a grid called gridpurposes with a label in the first column
        ' Dim lblPid As Label = Me.GridPurposes.SelectedItem.Cells(0).FindControl("lblPurposeID") 
        '  Dim strPid As String = lblPid.Text

 
        ' I found that after I pressed the button and it did a post back the ItemIndex 
        ' for the row  would get set to zero.
        ' I have used  the functions GetSelectedDataKey below 
        ' to get the right value. Notice that I am passing e.Item.ItemIndex to the
        ' Function so it uses that instead of selectedindex


    Dim myob = GetSelectedDataKey(Me.GridPurposes, e.Item.ItemIndex)       

    End Sub

Private Shared Function GetSelectedDataKey_Common( _
    ByRef dg As DataGrid, _
    ByVal ItemIndex As Integer) As Object
        Dim obj As Object = Nothing
        If Not dg Is Nothing Then
            Dim dgItem As DataGridItem = dg.Items(ItemIndex)
            If Not dgItem Is Nothing Then
                Dim i As Integer = dgItem.ItemIndex
                If i >= 0 Then
                    obj = dg.DataKeys(i)
                End If
            End If
        End If
        Return obj
    End Function

    Shared Function GetSelectedDataKey( _
    ByRef dg As DataGrid, _
    ByVal ItemIndex As Integer) As Object
        Return GetSelectedDataKey_Common(dg, ItemIndex)
    End Function

    Shared Function GetSelectedDataKey( _
    ByRef dg As DataGrid) As Object
        If Not dg Is Nothing Then
            Return GetSelectedDataKey_Common(dg, dg.SelectedIndex)
        Else
            Return Nothing
        End If
    End Function
>I think you'll want to look at the ItemCommand event instead of selected index changed because when you click you're dealing with button. This gives you a reference to the row, and from there you can access the data items and content of the row selected.
>
>
>protected void ItemCommand(object sender, DataGridCommandEventArgs e)
>{
>	// *** Button command Names contain Sku value if set
>	if (e.CommandName != null && e.CommandName != "" && e.CommandName != "Page")
>	{
>		// *** If we clicked on one of the items to buy - redirect to the item page
>		string sku = e.CommandName;
>		string qty = ((TextBox) e.Item.FindControl("qty")).Text;
>		if (sku != null && sku!="")
>		{
>			Server.Transfer("Item.aspx?sku=" + sku.Trim() + "&qty=" + qty + "&Action=Add");
>			//Response.Redirect("Item.aspx?sku=" + sku.Trim() + "&qty=" + qty + "&Action=Add");
>		}
>		
>	}
>}
>
>
>Note that you have to make sure you filter this correctly - especially filtering out Paging commands if your data grid has paging enabled.
>
>
>I would highly recommend you get a book for this as a reference. Dino Esposito's Professional ASP.Net has a lot of this stuff covered in a way that's both readable and easy to find for reference even though it's very thourough.
>
>I know I had a heck of a time with the DataGrid as well - basically it works great for the base stuff, but when you want to do more complex stuff you really have to know what events fire. Tough.
>
>+++ Rick ---
>
>
>>I always have this trouble with the controls in .net
>>
>>I can never find the value of whats stored in controls without lots of counter intuitive hoohah :(
>>
>>I always get lost in the watch window and spend hours looking for values that should be readily apparent.
>>
>>The example below is a case in point.
>>
>>I have a datagrid on a web form. This grid has has a button on each row in the form of some underlined text (this is a type of button not a URL type however).
>>
>>When the user presses a button I want to determine the value that is displayed on the button.
>>
>>In a combo box I would refer to this as the datatext field. I also want what in a combo would be called a datavalue field.
>>
>>Below I show the event that is getting fired when a user clicks one of my buttons.
>>It also shows that I am trying to acess the DataGridItem.
>>
>>I have dropped this "so called" datagrid Item (dgItem)onto a watch window but can see no dataitems in it.
>>
>>Trying to find my data in .net controls drives me nuts!
>>
>>
>>    Private Sub GridPurposes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridPurposes.SelectedIndexChanged
>>        Debug.WriteLine("grid clicked")
>>        Dim dgcollection As DataGridItemCollection
>>        Dim dgItem As DataGridItem = sender.Items(sender.SelectedIndex)
>>
>>    End Sub
>>
>>
>>
>>
>>I Just found the code below that indicates that the .cells.text  will return the value.
>>but this appeared blank "" to me in the watch window when I looked at it in debug window.
>>
>>I am at home now so will try this again tomorrow,
>>Any idea why they duplicated it when populating label1.text?
>>
>>    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged
>>        Label1.Text = "You Selected " & DataGrid1.SelectedItem.Cells(1).Text & " " & DataGrid1.SelectedItem.Cells(1).Text
>>        Label2.Text = "Selected Row Index is " & DataGrid1.SelectedIndex
>>    End Sub
>>
>>
>>
>>
Previous
Reply
Map
View

Click here to load this message in the networking platform