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

RebindGrid command and selection

3 Answers 40 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JP
Top achievements
Rank 1
JP asked on 02 Dec 2013, 10:40 AM
Hi,

I'm using a RadGrid with a GridClientSelectColumn.
If I call server-side the Grid.Rebind() method, I can preserve the selected items:
var selectedItems = Grid.MasterTableView.GetSelectedItems()
               .Select(item => int.Parse(item["idCol"].Text))
               .ToList();
Grid.Rebind();
foreach (GridDataItem item in Grid.MasterTableView.Items)
{
    item.Selected = selectedItems.Contains(int.Parse(item["idCol"].Text));
}

But how can I do this when the user clicks the "Update" button (which fires the RebindGrid command)?

Thanks!

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Dec 2013, 02:01 PM
Hi ,

Im not sure about your requirement, i guess you are using GridClientSelectColumn, to select some rows and you want it to be persisting across pages and on Update. Please try the below is a sample code snippet .

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" AllowMultiRowSelection="True"
    AutoGenerateEditColumn="true" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender" OnItemCreated="RadGrid1_ItemCreated">
    <MasterTableView DataKeyNames="CustomerID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
        </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true" Selecting-UseClientSelectColumnOnly="true">
    </ClientSettings>
</telerik:RadGrid>

C#:
ArrayList selectedItems;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session.Clear();
    }
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{     
    if (Session["selectedItems"] == null)
    {
        selectedItems = new ArrayList();
    }
    else
    {
        selectedItems = (ArrayList)Session["selectedItems"];
    }
    if (e.CommandName == "RowClick")
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string customerID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString();
        if (dataItem.Selected)
        {
            selectedItems.Add(customerID);
            Session["selectedItems"] = selectedItems;
        }
        else
        {
            selectedItems.Remove(customerID);
            Session["selectedItems"] = selectedItems;
        }
    }      
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (Session["selectedItems"] != null)
    {
        ArrayList selectedItems = (ArrayList)Session["selectedItems"];
        Int16 stackIndex;
        for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
        {
            string curItem = selectedItems[stackIndex].ToString();
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)item;
                    if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString()))
                    {
                        dataItem.Selected = true;
                        break;
                    }
                }
            }
        }
    }
}
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if (e.Item is GridHeaderItem)
  {
    GridHeaderItem headerItem = (GridHeaderItem)e.Item;
    CheckBox chk = (CheckBox)headerItem["ClientSelectColumn"].Controls[0];
    if (chk.Checked)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string customerID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString();
        if (dataItem.Selected)
        {
            selectedItems.Add(customerID);
            Session["selectedItems"] = selectedItems;
        }
    }
  }
}

Thanks,
Shinu
0
JP
Top achievements
Rank 1
answered on 05 Dec 2013, 12:24 PM
Thanks for your answer, I tried your approach and it is working but for one case: I want to get the SelectedRows in page's OnPreRender, but after the update command it still returns nothing because the Grid's PreRender is executed after the page's OnPreRender. I need some event handler where I can set the seelcted rows which is executed during every postback before the page's OnPreRender method. 

Thanks!
0
Eyup
Telerik team
answered on 10 Dec 2013, 08:48 AM
Hello Jan-Patrick,

You can check out the following sample:
http://www.telerik.com/community/code-library/aspnet-ajax/grid/get-selected-items-through-all-pages.aspx

Which uses this approach:
http://www.telerik.com/help/aspnet-ajax/grid-persist-selected-rows-client-sorting-paging-grouping-filtering.html

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
JP
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
JP
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or