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

Loop through selected items

3 Answers 533 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 07 Jan 2014, 07:40 PM

I have a radgrid and I can loo through the selected items.  If I set the for loop

for (int i = 0; i < totalSelected; i++)
     GridDataItem selectedItem = (GridDataItem) RadGrid1.SelectedItems[i]

it will loop in the order that they were selected and not the order of display on the screen.  Is there a way to loop through the selected based on the order in the grid?

for the time being I have found

foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
   if (item.Selected)
   {

but this will loop through the whole grid which seems kind of a waist when I should be able to just loop through the selected items.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Jan 2014, 05:04 AM
Hi Frank,

Generally, the SelectedItems collection of RadGrid returns the items in order of their selection time. Please try this following code snippet to get the index sorted to display the order as displayed in Grid:

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" AllowPaging="true"
    AllowMultiRowSelection="true" OnNeedDataSource="RadGrid1_NeedDataSource">
    <ClientSettings Selecting-AllowRowSelect="true">
    </ClientSettings>
</telerik:RadGrid>
<br />
<asp:Label ID="Label1" runat="server" Text="Click the button"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Get Data" OnClick="Button1_Click" />

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
        new { ID = 1, Name = "Name1"},
        new { ID = 2, Name = "Name2"},
        new { ID = 3, Name = "Name3"},
        new { ID = 4, Name = "Name4"},
        new { ID = 5, Name = "Name5"},
        new { ID = 6, Name = "Name6"},
        new { ID = 7, Name = "Name7"},
        new { ID = 8, Name = "Name8"},
        new { ID = 9, Name = "Name9"}
    };
    RadGrid1.DataSource = data;
}
protected void Button1_Click(object sender, EventArgs e)
{  
    List<int> indices = new List<int>();
    foreach (GridDataItem item in RadGrid1.SelectedItems)
    {
        indices.Add(item.ItemIndex);
    }
    indices.Sort();
    Label1.Text = "<b>Selected:</b><br/>" + string.Join("<br/>", indices.ToArray());
}

Thanks,
Princy
0
Frank
Top achievements
Rank 1
answered on 08 Jan 2014, 05:47 PM

Nope that has the same problem.

What is happening is on this screen is a grid of documents grouped be clients that these documents belong to.  Each client has a distinct email and distinct documents in their group. On the screen I allow the user to enter email body, and select a signature, then allow them to add ccs and other attachments to the email.  Then they select what documents for each client they want to include in the emails.

Once all this is done they hit send emails and the app will loop through the grid merger the documents for each client into on document create the email and send emails to each client.

I have included a sample grid with some edits

(see image to follow)

What happens is the user will sometime say click two of the documents for client 3, then one for client 5, then one for client 2., then click the 3rd document for client 3 , then click the 2 for client 1, the click the last 2 for client 2.

Then they hit the send email button.

By using the selectedItems it creates the email in that order the order the documents were selected so for the above example

client 3 gets an email with the 2 documents merged

client 5 gets an email

client 2 gets an email

client 3 gets another email for the 3rd document

client 1 gets and email with 2 documents merged

client 2 gets another email with the other 2 documents merged.

They only way I have found to not get this behavior is to loop through the entire grid look at each line to see if they are selected as show in the first post.

Is there a way to loop through just the selected but in the way they appear in the grid and not in the order they were selected by the user?

0
Accepted
Viktor Tachev
Telerik team
answered on 10 Jan 2014, 02:24 PM
Hi Frank,

Indeed GridDataItems are added in the SelectedItems collection in the order they were selected. In order to have the items sorted you could use the following approach. Store the selected items in a collection. Items would be added on Select command and removed on Deselect command. When you need the data you could sort the selected GridDataItems and then use them as needed.

As illustration I have prepared a sample project. I am using a session variable to store the selected items. When the Show selected button is clicked the previously selected items are displayed in the grid in sorted order.

Regards,
Viktor Tachev
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
Frank
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Frank
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or