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

How to select a row via server code?

1 Answer 84 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 24 Mar 2011, 03:33 PM
I have a combo-box which provides a list of objects. When the user selects the item in the combo-box, it is bound to an ObjectDataSource. That ObjectDataSource is bound to a RadGrid which auto-populates the grid. This works great.

In this scenario, how do we get the first row of that grid to be selected whenever the user picks a new value in the combo-box? We have tried the grid's pre-render event and the ObjectDataSource's selected event. None of these work as the data has not yet been populated into the grid. 

We also tried the grid's ItemBound event; however, it fires with every event on the grid it seems. We only need it to set the first row when new data has been bound.

Any suggestions on which event to use?

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Mar 2011, 06:10 AM
Hello Brian,

Try the following approach ans see if it works.
ASPX:
<telerik:RadComboBox runat="server" ID="RadComboBox1" AutoPostBack="true"
        OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
    <Items>
       .   .   .
    </Items>
</telerik:RadComboBox>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="True" 
        OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadGrid1.Rebind(); // will call NeedDataSource event
    }
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
    //based on SelectedValue of comboBox populate RadGrid
        if (RadComboBox1.SelectedValue == "value")
           //populate RadGrid
    }
    
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
           RadGrid1.MasterTableView.Items[0].Selected = true;//select first row
    }

Thanks,
Princy.
Tags
Grid
Asked by
Brian
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or