Visual Studio .NET - why is GridView3 SelectedDataKey Value null when

Asked By Paddy Mac on 09-Nov-05 09:55 AM
I change the select command of a data source?

I have a data grid which has a data source assigned to it. On the page load event I change the data source SelectCommand to use a different table with the same fields in it. 

 Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dsFileRegMain.SelectCommand = "my SELECT command " 
End Sub

The datagrid displays correctly. but when I use the OnSelectedIndexChange event I get an error on  the selecteddatakey value?  

Session("SelectedFileRegRecordNo") = GridView3.SelectedDataKey.Value.ToString()

The SelectedIndex works ok but the selected date key doesn't.  I'm not sure why?  the Data Key is set ok. and the datagrid work ok if I don't change it in the page load event. 

I've had this problem for a while now but I'm clueless at moment.

Help please.


SelectedDataKey Value null - Asked By Apocalypse Angel on 12-Nov-05 01:56 AM

Hi paddy.

I had the same problem with ASP.NET 2.0. Frankly I don't know why ASP is not updating the Grid object when he enter the EVENT OnSelectedDataKey. Probably a bug. I took the same code of an other project and fall on that bug with no reason.

What I did to "bypass" the bug is calling the DataBind() method of the Grid just before using the SelectedDataKey property. I though it would update itself and it did. 
(it's just too bad to use some machine time to bypass the bug.)

Hope it help :)

-here's my code-

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        if (((GridView)sender).SelectedDataKey != null)
            String primaryKey;
            primaryKey = ((GridView)sender).SelectedDataKey.Value.ToString();


SelectedDataKey Value null - Asked By Apocalypse Angel on 12-Nov-05 05:51 PM

It's me again. I just wanted to tell you that all my Select, Update and Delete button work in the grid after I updated FrameWork 2.0 beta2 to the release version.

good luck !

Thanks Angel - Asked By Paddy Mac on 15-Nov-05 03:38 AM

sorry I didn't get back for a while. on leave.

This is very useful.