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

Autoselect first row in grid

8 Answers 248 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SikhSuperman
Top achievements
Rank 1
SikhSuperman asked on 17 Oct 2012, 10:25 PM
Hello,

I need to have the ability to select the first row in a grid automatically and based on that selection two other radgrids gets populated.  I was able to achieve the selection of the first row by doing the following in the prerender method.

This worked somewhat.  (1) The two other grids are not populated when the autoselection happens, and (2) If i select another row in RadGrid1, the new selection is not kept and isntead the row from the prerender method is always selected.

I had all this working when a user comes in selects from a dropdown, but it seems that the client wants the first selection in the radcombobox to be automatically selected with the relevant data...

Any help would be tremendous, please!

SS
' PreRender Method
 
if (RadGrid1.MasterTableView.Items(0).Count > 0) Then
     RadGrid1.MasterTableView.Items(0).Selected = True
End If

8 Answers, 1 is accepted

Sort by
0
SikhSuperman
Top achievements
Rank 1
answered on 18 Oct 2012, 01:41 PM
Hey folks -
Anybody have any idea that can get me moving in the right direction? Please?
0
Casey
Top achievements
Rank 1
answered on 18 Oct 2012, 02:42 PM
Hello,

To prevent the first item from always being selected, you should only set it selected if there are not any items selected in the RadGrid already.

I hope this helps!
Casey

If (RadGrid1.MasterTableView.Items(0).Count > 0) Then
 
     If (RadGrid1.SelectedItems.Count ==  0) Then
 
     RadGrid1.MasterTableView.Items(0).Selected = True
 
     End If
     'You could rebind your other RadGrids here since you will have an item selected in the first RadGrid.
     'Then in the NeedDataSource for your other RadGrids, use the values from the item that is selected in RadGrid1
End If
0
SikhSuperman
Top achievements
Rank 1
answered on 18 Oct 2012, 03:11 PM
Hi Casey,

Thanks so much for the reply.  The selection works as intended (I do want the first row selected upon entering the page and radgrid).

I did what you suggested below, but for some reason when I do the Rebind() to the other grids, the _ItemDatabound fires for the grids instead of the _NeedsDataSource events...any idea why?  I thought that the Rebind() automatically fires the _NeedsDataSource by design?
If (grd1s.MasterTableView.Items.Count > 0) Then
    If (grd1.SelectedIndexes.Count = 0) Then
        grd1.MasterTableView.Items(0).Selected = True
        grd1.Enabled = True
    End If
        radGrid2.Rebind()
        radGrid3.Rebind()
 End if
Thanks again!
SS
0
Casey
Top achievements
Rank 1
answered on 18 Oct 2012, 03:18 PM
That is correct. The NeedDataSource events for RadGrid2 and RadGrid3 should be hit when calling the Rebind() method. Could you please post the definitions for RadGrid2 and RadGrid3, as well as their respective NeedDataSource events? 

Thanks!
Casey
0
SikhSuperman
Top achievements
Rank 1
answered on 18 Oct 2012, 03:30 PM
Thanks Casey for the continued help!
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
     If (ID > 0) Then
         Dim ds As DataSet
         ds = xClass.GetX(EventsID)
         RadGrid1.DataSource = ds.Tables(0)
     Else
         'Set empty grid
         RadGrid1.DataSource = New Integer() {}
     End If
 
Protected Sub RadGrid2_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource
     If (ID IsNot Nothing) Then
         Dim ds As DataSet
         ds = xClass.GetX(EventsID)
         RadGrid2.DataSource = ds.Tables(0)
     Else
         'Set empty grid
         RadGrid2.DataSource = New Integer() {}
     End If
 
 Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
     If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
         Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
         Dim txtO As RadTextBox = item.FindControl("txtO")
         Dim rdpDate As RadDatePicker = item.FindControl("txtDate")
         If (txtO IsNot Nothing) Then
             txtO.Focus()
         End If
     End If
 End Sub
 End Sub
 
 End Sub
The RadGrid2_ItemDatabound is the same as RadGrid1 except 'txtO' is now 'txtG'.

Thanks
SS
0
Casey
Top achievements
Rank 1
answered on 18 Oct 2012, 08:14 PM
Are you using a value from the selected row in RadGrid1 to pass to a method to get the data for RadGrid2? I'm a little lost because I'm unsure of where you are setting the values for ID and EventsID.  

Could you also post your RadGrid2 definition from the aspx page? 

Thanks!
Casey
0
Radoslav
Telerik team
answered on 22 Oct 2012, 08:45 AM
Hello Jag,

Could you please check out the following example which demonstrates the desired functionality and let me know if it helps you:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/selectedvalue/defaultcs.aspx

Looking forward for your reply.

All the best,
Radoslav
the Telerik team
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 their blog feed now.
0
SikhSuperman
Top achievements
Rank 1
answered on 24 Oct 2012, 01:09 PM
Thanks Casey and all for the continued help.
I managed to figure out why the _NeedsDataSource event wasn't firing.  It was because the grid was getting bound to a datasource early on in code and therefore the _NeedDataSource event need not be fired.  I re-set the datasource for the affected grid to an empty datasource and re-ran the code and viola the _NeedsDataSource event fired.

Thanks again folks!!
SS
Tags
Grid
Asked by
SikhSuperman
Top achievements
Rank 1
Answers by
SikhSuperman
Top achievements
Rank 1
Casey
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or