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:
00868358
Views:
20
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
>
>
>
>
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform