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"); > } > > } >} >>
>> 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 >> >> >> >>