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

RadGrid pre-selected rows

1 Answer 219 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sunil
Top achievements
Rank 1
Sunil asked on 11 Mar 2015, 05:34 PM
Hello,

I have a radgrid in my aspx file that allows for selecting multiple rows with a checkbox.  I am using the two properties:

AllowMultiRowSelection="true" and 

<ClientSettings EnableRowHoverStyle="true">
     <Selecting AllowRowSelect="True"></Selecting>
 </ClientSettings>

In my code behind, I populate the grid using a datasource as follows:

CogCustomCompetenciesGrid.DataSource = cl.OrderByDescending(p=> p.ImportanceRating).ToList();

The list used for population consists of objects with multiple properties.  One of the properties is a boolean called PreviouslySelected.  

Based on whether the PreviouslySelected property is true or false, I need to pre-set the checkmark selection for that particular row.

How do I do this?













1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 16 Mar 2015, 09:09 AM
Hi Sunil,

For selecting the rows depending on a boolean value you could handle the server-side OnItemDataBound event of the grid, retrieve the bool value and set the Selected property of the GridDataItems according to that value.

For your convenience, following is a simple example demonstrating such implementation, where the boolean data field is included in the DataKeyNames collection, so it could be easily retrieved from the item:
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound"
    AllowMultiRowSelection="true"
    <MasterTableView DataKeyNames="PreviouslySelected"></MasterTableView>      
    <ClientSettings>
        <Selecting AllowRowSelect="true" />
    </ClientSettings>
</telerik:RadGrid>

And the code-behind:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("PreviouslySelected", typeof(Boolean));
    for (int i = 1; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
    }
 
    (sender as RadGrid).DataSource = table;
}
 
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        if (item.GetDataKeyValue("PreviouslySelected").ToString()  == "True")
        {
            item.Selected = true;  
        }
    }
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Sunil
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or