views:

1161

answers:

3

I have standard .net 2.0 gridview control from which i want to get row keys or cell values from the grid when a row is selected.

I also need to call a method each time a row is selected.

Does anybody know How i can do this using ASP.net & VB?

A: 

Add a button or link button to your gridView.

For the command name put in "Select"

In the designer window, double click your gridView and your selected index changed method will be automatically generated.

Eppz
+2  A: 

I assume you mean selection by the Select command. That fires a SelectedIndexChanged event. and from there on you have the SelectedDataKey.Value (or .Values) for the Key.

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, _
            ByVal e As EventArgs) _
            Handles GridView1.SelectedIndexChanged
    Dim key As Object = GridView1.SelectedDataKey.Value
End Sub
Henk Holterman
Hi i did this and it almost works... when i step through the code it does seem to capture the key values but then gives this error message"Object reference not set to an instance of an object"any idea whats wrong?
eMTeeN
any idea whats wrong? -- not until you tell us which object is null.
Henk Holterman
it doesnt say... heres the code i have put in the SelectedIndexChangedMethod... Dim key As Object = GridView1.SelectedDataKey.Value txtTest.Text = key
eMTeeN
You have a debugger, it can tell you who is null. Do a check on key is Nothing, it could sometimes be Null. If it is always null there is another problem.
Henk Holterman
The problem i was having was that i was dynamically setting the Data property of the XMLdatasource but had set the gridview EnableViewState Property to false. This some sort of internal problem within the Gridview which fixed itself when i set the EnableViewState to true.
eMTeeN
Ok, but keep an eye on the size of your viewstate.
Henk Holterman
A: 

I prefer using third-party grid like the Telerik one which allows server and client selection as well as postback on click to process some logic.

Dick