This is a migrated thread and some comments may be shown as answers.

RadGrid : Problem setting the SelectCommand

2 Answers 236 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 29 Jun 2011, 03:27 AM
I have created a page with a Master / Detail pair of grids with the help of the excellent tutorial here on the Telerik site.

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/selectedvalue/defaultcs.aspx

I got this working fine from the start. I have the top row in the grid highlighting when the page loads and the details grid responds with the correct data for that grid. Selecting another row on the master works the same way. The details grid updates and joy and happiness abound. :)

The top grid has a simple SqlDataSource that looks like this.

<asp:SqlDataSource ID="dsLocations" runat="server" ConnectionString="<%$ ConnectionStrings:Database_DB1 %>"
        SelectCommand = "select * from GetLocations WHERE loc_User_ID = 44"
        ProviderName="System.Data.SqlClient" >
</asp:SqlDataSource>

As you can see I am hardcoding the userID. This is my test of the system and it works.

It is when I try to set this dynamically the problems start.

First I deleted the SelectCommand parameter from the SqlDataSource above.

<asp:SqlDataSource ID="dsLocations" runat="server" ConnectionString="<%$ ConnectionStrings:Database_DB1 %>"
        ProviderName="System.Data.SqlClient" >
</asp:SqlDataSource>

Then in the Page_PreRender  of the code behind I do this. .

protected void Page_PreRender(object sender, EventArgs e)
{
    dsLocations.SelectCommand = "select * from GetLocations WHERE loc_User_ID = " + user.User_ID.ToString();
 
    if (rgLocations.SelectedIndexes.Count == 0)
    {
        rgLocations.SelectedIndexes.Add(0);
        rgActivities.Rebind();
    }
}

This works in as much as the correct Locations for that UserID are returned.

However there are 3 problems that appear
  • the first row of the grid is no longer longer highlighted ie: rgLocations.SelectedIndexes.Add(0); // fail
  • the details grid is no longer showing the details for the top row on page load id: rgActivities.Rebind(); // fail
  • finally the details grid no longer updates when you select a row from the master table.

I did not bore you with pasting my grids here. Suffice to say they work, as long as I hard code the SelectCommand in the page.

Perhaps Page_PreRender is not the correct place to set the SelectCommand on the data source?

Could someone please shed some light on what is causing this behavior?






2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 29 Jun 2011, 08:22 AM
Hello Brad,

Try the following code snippet in Page_Load event which worked as expected.

C#:
protected void Page_Load(object sender, EventArgs e)
   {   
   SqlDataSource1.SelectCommand = "SELECT * FROM Employee ";
           if (RadGrid1.SelectedIndexes.Count == 0)
           {
               RadGrid1.SelectedIndexes.Add(0);
           }
   }

Thanks,
Shinu.
0
Brad
Top achievements
Rank 1
answered on 29 Jun 2011, 11:39 PM
Thanks Shinu

I'm not sure why this did not work in Page_PreRender. None the less it's working now. Thanks again.

Tags
General Discussions
Asked by
Brad
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Brad
Top achievements
Rank 1
Share this question
or