Select a row programmatically in a RadGrid

6 posts, 1 answers
  1. Chad Johnson
    Chad Johnson avatar
    17 posts
    Member since:
    Apr 2010

    Posted 25 Apr 2012 Link to this post

    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.




  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 26 Apr 2012 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Chad Johnson
    Chad Johnson avatar
    17 posts
    Member since:
    Apr 2010

    Posted 26 Apr 2012 Link to this post

    That's exactly what I needed...thank you so much Jayesh!
  5. Jaime Bula
    Jaime Bula avatar
    41 posts
    Member since:
    Apr 2009

    Posted 28 May 2014 Link to this post

    Getting "Please set RadGrid AllowMultiRowSelection to "True" to start selecting multiple items. "

    is there a simple way to select the first row by default?
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 May 2014 in reply to Jaime Bula Link to this post

    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
  7. Andres
    Andres avatar
    1 posts
    Member since:
    Jun 2014

    Posted 14 Jul in reply to Jayesh Goyani Link to this post

    Muy buen aporte me yudo gracias
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017