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

Issue Selecting a grid Item after a sort is performed

1 Answer 30 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 07 Jan 2009, 12:36 AM
Hi,
I am encountering issues with my grid after I perform a sort.
I have a web page that binds a <List> of objects to my datagrid in the page load. I also store this list in the users session for later use when I want to perform selecting.

List<Invoice> outstandingInvoices = InvoiceInfo.GetOutstandingInvoicesByDebtor(1);
Session["outstandingInvoices"] = outstandingInvoices;
rg_Invoices.DataSource = outstandingInvoices;

After the user has select items in rg_Invoices they click a select button and the invoices they selected placed in rg_Assigned and Removed from rg_Invoices
            //create a list to store what was selected
            List<InvoiceInfo> assignedInvoices = (List<InvoiceInfo>)Session["assignedInvoices"];
            //read in the original list from the session
            List<InvoiceInfo> outstandingInvoices = (List<InvoiceInfo>)Session["outstandingInvoices"];
            //Create a new outstandingInvoicesList
            List<InvoiceInfo> newOutstandingInvoices = new List<InvoiceInfo>();

            foreach (InvoiceInfo inv in outstandingInvoices)
            {
                bool test = false;
                //compare it against the selected items in the grid
                foreach (GridDataItem item in rg_Invoices.SelectedItems)
                {
                    InvoiceInfo invoice = (InvoiceInfo)outstandingInvoices[item.ItemIndex];
                    if (inv == invoice)
                    {
                        //if it finds it then add the obj to assigned
                        assignedInvoices.Add(invoice);
                        test = true;
                    }
                }
                if (!test)
                    newOutstandingInvoices.Add(inv); //if not found add to new matches
            }

            Session["assignedInvoices"] = assignedInvoices;
            rg_Assigned.DataSource = assignedInvoices;
            rg_Assigned.DataBind();

            Session["outstandingInvoices"] = newOutstandingInvoices;
            rg_Invoices.DataSource = newOutstandingInvoices;
            rg_Invoices.DataBind();

This all works fine however if I perform a sort on the grid and then try so select items from rg_Invoices into rg_Assigned then the wrong invoice gets brought across.

My code to sort the datagrid is
protected void rg_Invoices_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
            List<InvoiceInfo> outstandingInvoices = (List<InvoiceInfo>)Session["outstandingInvoices"];
            rg_Invoices.DataSource = outstandingInvoices;
}

The sort works fine but it appears that the sort only works client side and the list of invoices in my session does not get sorted therefore they then become out of synch when I try to select.

Is there any way that I can set my session to the sorted format in the NeedDataSource method??

Any help would be greatly appreciated.

Thanks,
Michael

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 09 Jan 2009, 01:39 PM
Hello Kevin,

I would suggest that you employ advanced databinding for your setup, to avoid having the items out of synch.

Kind regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or