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

Select a row programmatically in a RadGrid

7 Answers 3874 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chad Johnson
Top achievements
Rank 1
Chad Johnson asked on 25 Apr 2012, 04:39 PM
I am querying my database and returning a dataset of ClientIdentifier objects to bind to a RadGrid.  Each ClientIdentifier object has a boolean property IsDefault.

I am binding the dataset to the RadGrid using the NeedDataSource event.  I am not including displaying the IsDefault property in the RadGrid.  I am displaying a GridClientSelectColumn instead.

What I would like to do is select the row in the RadGrid that corresponds to the ClientIdentifier object with IsDefault = true programmatically when the grid is first loaded.  After the RadGrid is loaded, the user can then select whatever item they want in the RadGrid.

Here is my RadGrid declaration:
<telerik:RadGrid ID="radGridClientIdentifiers2" runat="server" AutoGenerateColumns="false"
    OnUpdateCommand="radGridClientIdentifiers2_UpdateCommand"
    OnDeleteCommand="radGridClientIdentifiers2_DeleteCommand" OnInsertCommand="radGridClientIdentifiers2_InsertCommand" OnItemCreated="radGridClientIdentifiers2_ItemCreated" OnDataBound="radGridClientIdentifiers2_DataBound"
    OnNeedDataSource="radGridClientIdentifiers2_NeedDataSource">
    <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="PushButton" UniqueName="EditCommandColumn" />
            <telerik:GridBoundColumn HeaderText="Number" DataField="Identifier" />
            <telerik:GridDropDownColumn HeaderText="Type" EnableEmptyListItem="true" DataField="IdentifierTypeID" ListTextField="Description" ListValueField="ID" DataSourceID="objectDataSourceClientIdentifierTypes" />
            <telerik:GridButtonColumn UniqueName="DeleteColumn" ButtonType="ImageButton" CommandName="Delete" />
            <telerik:GridClientSelectColumn UniqueName="SelectColumn" HeaderText="Default" />
        </Columns>
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <telerik:RadTextBox ID="radTextBox" Label="Number:" Text='<%# Bind("Identifier") %>' runat="server">
                </telerik:RadTextBox>
                <telerik:RadComboBox ID="radComboBox" runat="server" Label="Type:" SelectedValue='<%# Bind("IdentifierTypeID") %>' DataSourceID="objectDataSourceClientIdentifierTypes" DataTextField="Description" DataValueField="ID" />
                <telerik:RadButton ID="radButtonInsert" runat="server" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />
                <telerik:RadButton ID="radButtonCancel" runat="server" Text="Cancel" CausesValidation="false" CommandName="Cancel" />
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
    <ClientSettings>
        <Selecting AllowRowSelect="true" />
    </ClientSettings>
</telerik:RadGrid>

How can I go about doing this?  I've been playing around with the RadGrid's DataBound event with little luck.  I'm not sure that's even the right event to use.




7 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Apr 2012, 05:20 AM
Hello Chad,


protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                if (item.GetDataKeyValue("ID").ToString() == "1")
                {
                    item.Selected = true;
                }
            }
        }


<MasterTableView  DataKeyNames="ID">

OR

protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                if (item["ID"].Text == "1")
                {
                    item.Selected = true;
                }
            }
        }

<telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                  </telerik:GridBoundColumn>


Thanks,
Jayesh Goyani
0
Chad Johnson
Top achievements
Rank 1
answered on 26 Apr 2012, 12:47 PM
That's exactly what I needed...thank you so much Jayesh!
0
Jaime Bula
Top achievements
Rank 2
answered on 28 May 2014, 03:59 PM
Getting "Please set RadGrid AllowMultiRowSelection to "True" to start selecting multiple items. "

is there a simple way to select the first row by default?
0
Princy
Top achievements
Rank 2
answered on 29 May 2014, 03:40 AM
Hi Jaime Bula,

You can set the following code snippet to have the first row selected.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  RadGrid1.MasterTableView.Items[0].Selected = true;
}

Thanks,
Princy
0
Andres
Top achievements
Rank 1
answered on 14 Jul 2016, 03:52 PM
Muy buen aporte me yudo gracias
0
Martin
Top achievements
Rank 1
answered on 13 Jul 2017, 02:46 PM

Hello Yayesh,

Your solutions works fine if you don't use paging. Do you know what to do to make it work if f.e. the row to be selected is on page 3?

Thanks.

Regards,

Martin

0
Marin Bratanov
Telerik team
answered on 18 Jul 2017, 01:10 PM

Hi Martin,

The grid only fetches and renders the items for the current page, so selecting items on other pages is not possible directly.

You could take the approach from the following article to store selected IDs so you can check against them when the grid renders: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Selecting/persisting-selected-rows-server-side. Similar logic can be implemented on the client-side as well: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Selecting/persisting-selected-rows-client-side.

Regards,

Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Chad Johnson
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Chad Johnson
Top achievements
Rank 1
Jaime Bula
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Andres
Top achievements
Rank 1
Martin
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or