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

Set selectedvalue programmatically

2 Answers 341 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 25 Apr 2012, 04:14 PM
Hi I am trying the set the SelectedValue in the item databound event as follows
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
       If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
            Dim editform As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
           Dim combo As RadComboBox = DirectCast(editform.FindControl("RadComboBox1"), RadComboBox)
           combo.SelectedValue = "123
       End If
End Sub

and this works as it is, but I actually want to have the SelectedValue set to a specific column value of the selected row, but I am not sure how I can get this value at this point

Basically I am trying to replicate what happens declaratively when you have SelectedValue='<%# Bind("ColumnName") %>'  but be able to intercept the value in case it does not exist in the list of values in the combobox and causes the 'Out of range' error.

Please advise !

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Apr 2012, 10:23 AM
Hello Clive,

Try binding the selected value as shown below.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound 
       If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
            Dim editform As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
           Dim combo As RadComboBox = DirectCast(editform.FindControl("RadComboBox1"), RadComboBox)
          combo.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "FirstName").ToString(), String)
       End If
End Sub

Thanks,
Shinu.
0
Mike
Top achievements
Rank 2
answered on 04 May 2012, 06:06 PM
I'm having the issue. I have a grid with a RadComboBox control in a column which works fine unless the value in the column no longer exists in the data source for the dropdown. Then I get this error:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Selection out of range
Parameter name: value

I tried Shinu's suggestion but it hits the error before getting to the ItemDataBound event for the edited record. Does anyone have a way around this error. I need to be able to change the value of the combobox if it no longer exists in the data source.

UPDATE: I found a way around this. My solution was a little different because I was dealing with a combobox within a grid item so instead of GridEditFormItem I used GridEditableItem. With the code below if the person no longer exists in the dropdown it just leaves it blank and does not throw the error.

If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
 
    Dim combo As RadComboBox = DirectCast(editItem.FindControl("RadComboBox1"), RadComboBox)
 
    combo.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "StaffID").ToString(), String)
 
End If


Tags
ComboBox
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mike
Top achievements
Rank 2
Share this question
or