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

SelectedValue is incorrect, but corrected based on record in that "place" if the grid were to be rebound.

3 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 12 Jul 2013, 07:10 PM
I have a RadGrid that uses a SQLDataSource.
I have a Select button in the grid, code at the bottom.

In the SelectedIndexChanged event for the grid I am capturing the .SelectedValue and using that to perform other SQL commands.
Issue is that, as an example, if I select the first row in my Grid but the database has been updated to have another row that would be before it (based on order/filtering rules) the selectedvalue that is given is this new record.

I figure it must have something to do with the "NeedDataSource" event, but I can't figure out what...  I would think that if I select a value for a grid, there should never be any rebinding done until after that is already selected, else I run into problems such as this...


<telerik:GridButtonColumn Text=">" CommandName="Select">
      <HeaderStyle Width="10px" />
      <ItemStyle Width="10px" />
</telerik:GridButtonColumn>

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 17 Jul 2013, 10:21 AM
Hello,

You should always use the primary key filed from the database for any database related operations. The primary key will be always unique and thus it will point to the same record always. Thus you would not get a new record instead of the old one.

When you perform some database operation with RadGrid it implicitly call Rebind() method which fetches the new data. This is done internally and could not be easily canceled.

I hope the provided information helps. Please elaborate a bit more if your scenario is different.

Regards,
Andrey
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Robert
Top achievements
Rank 1
answered on 17 Jul 2013, 01:28 PM
I am using a primary key.  Let's use this very basic table as an example.
Two values, a primary key and a date.

1 -> 7/10/13
2 -> 7/11/13
3 -> 7/12/13

In the RadGrid SQLDataSource, I am ordering this by Date Descending.
The primary key is not shown as a column, and is instead the data key name with a "Select Command" which triggers the SelectedIndexChanged event.

So my Rad Grid looks like:
7/12/13
7/11/13
7/10/13

If you <select> the first row, my RadGrid.SelectedValue would be 3.

Now let's pretend that I load the page again and wait 5 minutes without doing anything.
My Rad Grid still looks like above (3 items).
But my SQL table has another record added:
4 -> 7/13/13

Based on the datasource, this value would be the first one to display; however, it's obviously not displayed because I haven't reloaded the page.

When I "Select" the first record, I still expect to get a SelectedValue of 3 because that is the record that I can see and obviously clicked on.
Instead, I'm getting a SelectedValue of 4, (the new record) which is not visible anywhere on the grid.
It's as if the logic first sees "Ok you selected Index 0.... rebind the data so that it is completely different then when you selected it, and pick the new record at Index 0." 

Assuming this is what it is really doing, and for some reason intended, I do need a way to cancel this rebind. 
0
Andrey
Telerik team
answered on 22 Jul 2013, 08:06 AM
Hello,

Yes, you are correct, the selection of RadGrid items is made by indexes. This is done because if another person is modifying the database in the same as in your case the record you selected could be moved to a different page and different position. In order to find that record and select it again we will need to traverse the whole database and then calculate on which page the item will be and navigate to that page and select it. As you understand this will cause decrease in the performance for all users no matter many of them won't need this functionality.

In this case you could implement this feature manually, you could store the value of the DataKeyValue in some sort of collection and then you could traverse the datasource for these items and select them. Would you navigate to a different page if the item is not on the same page it is up to you to decide. I am attaching a sample project from our SDK that demonstrates the approach for hierarchy relation, however you could use for your case also.

Regards,
Andrey
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Robert
Top achievements
Rank 1
Share this question
or