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

Export Selected Rows Accross Mulitple Pages

1 Answer 22 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 06 Sep 2013, 02:45 PM
I am working with a grid that has paging and I want to be able to select multiple items across multiple pages and export those items. I have implemented the follow logic to export just selected items
 protected void Button1_Click(object sender, EventArgs e) 
    { 
          foreach (GridDataItem item in RadGrid1.Items)  
        {  
            if (item.Selected==false)  
            {  
                item.Visible = false;  
            }  
        }  
        RadGrid1.ExportSettings.ExportOnlyData = true;  
        RadGrid1.ExportSettings.OpenInNewWindow = true;  
        RadGrid1.MasterTableView.ExportToExcel();  
    } 

However, this only works if the grid export settings IngorePaging = False, in which case it only exports the selected items on the current page. If I set IgnorePaging=True, all items are exported all the time.

Is there a way to export selected items across multiple pages?

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Sep 2013, 10:06 AM
Hello,

Client Side
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
            new { ID = 1, Name ="aaa"},
            new { ID = 2, Name = "bbb"},
            new { ID = 3, Name = "ccc"},
            new { ID = 4, Name = "ddd"},
            new { ID = 5, Name ="eee"},
            new { ID = 6, Name ="aaa"},
            new { ID = 7, Name = "bbb"},
            new { ID = 8, Name = "ccc"},
            new { ID = 9, Name = "ddd"},
            new { ID = 10, Name ="eee"}
          };
       RadGrid1.DataSource = data;
 
 
   }
 
 
 
   protected void Button1_Click(object sender, EventArgs e)
   {
       RadGrid1.AllowPaging = false;
       RadGrid1.Rebind();
 
       if (!string.IsNullOrEmpty(HiddenField1.Value))
       {
           foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               if (HiddenField1.Value.IndexOf(","+item.GetDataKeyValue("ID").ToString()+",") == -1)
               {
                   item.Visible = false;
               }
               else
               {
                   item.Visible = true;
 
               }
           }
 
       }
 
 
       RadGrid1.ExportSettings.ExportOnlyData = true;
       RadGrid1.ExportSettings.OpenInNewWindow = true;
       RadGrid1.MasterTableView.ExportToExcel();
 
       RadGrid1.AllowPaging = false;
 
 
   }
<telerik:RadCodeBlock ID="telerikodeBlock1" runat="server">
      
       <script type="text/javascript">
           var selected = {};
           function RadGrid1_RowSelected(sender, args) {
               var mailID = args.getDataKeyValue("ID");
               AddorRemoveIdInHiddenField($("#HiddenField1").get(0), mailID, true);
           }
           function RadGrid1_RowDeselected(sender, args) {
               var mailID = args.getDataKeyValue("ID");
               AddorRemoveIdInHiddenField($("#HiddenField1").get(0), mailID, false);
           }
           function RadGrid1_RowCreated(sender, args) {
               var mailID = args.getDataKeyValue("ID");
               var hf = $("#HiddenField1").get(0);
               if (hf.value.indexOf("," + mailID + ",") >= 0) {
                   args.get_gridDataItem().set_selected(true);
               }
           }
           function GridCreated(sender, eventArgs) {
               var masterTable = sender.get_masterTableView(),
             headerCheckBox = $telerik.$(masterTable.HeaderRow).find(":checkbox")[0];
 
               if (headerCheckBox) {
                   headerCheckBox.checked = masterTable.get_selectedItems().length == masterTable.get_pageSize();
               }
           }
           function AddorRemoveIdInHiddenField(hf, id, IsAdd) {
 
               if (hf.value == "") {
                   hf.value = ",";
               }
 
               if (IsAdd == true) {
                   if (hf.value.indexOf("," + id + ",") == -1) {
                       hf.value = hf.value + id + ",";
                   }
               }
               else if (IsAdd == false) {
                   if (hf.value.indexOf("," + id + ",") >= 0) {
                       hf.value = hf.value.replace("," + id + ",", ",");
                   }
               }
 
           }
       </script>
       <script type="text/javascript">
           function onRequestStart(sender, args) {
               if (args.get_eventTarget().indexOf("ExportTo") >= 0) {
                   args.set_enableAjax(false);
               }
           }
       </script>
   </telerik:RadCodeBlock>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
  </telerik:RadScriptManager>
  
  <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
      <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
      <AjaxSettings>
          <telerik:AjaxSetting AjaxControlID="RadGrid1">
              <UpdatedControls>
                  <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
              </UpdatedControls>
          </telerik:AjaxSetting>
      </AjaxSettings>
  </telerik:RadAjaxManager>
  <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
  </telerik:RadAjaxLoadingPanel>
  <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
      AllowMultiRowSelection="true" PageSize="3" AllowPaging="true">
      <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID" ClientDataKeyNames="ID">
          <Columns>
              <telerik:GridClientSelectColumn>
              </telerik:GridClientSelectColumn>
              <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
              </telerik:GridBoundColumn>
              <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
              </telerik:GridBoundColumn>
              <telerik:GridEditCommandColumn>
              </telerik:GridEditCommandColumn>
          </Columns>
      </MasterTableView>
      <ClientSettings AllowDragToGroup="true">
          <Selecting AllowRowSelect="true" />
          <ClientEvents OnRowCreated="RadGrid1_RowCreated" OnRowSelected="RadGrid1_RowSelected"
              OnRowDeselected="RadGrid1_RowDeselected" OnGridCreated="GridCreated" />
      </ClientSettings>
  </telerik:RadGrid>
  <br />
  <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  <asp:HiddenField ID="HiddenField1" runat="server" />


Server Side
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
 {
 
     dynamic data1 = new[] {
         new { ID = 1, Name ="Name_1"},
         new { ID = 2, Name = "Name_2"},
         new { ID = 3, Name = "Name_1"},
         new { ID = 4, Name = "Name_4"},
         new { ID = 5, Name = "Name_1"}
     };
 
     RadGrid1.DataSource = data1;
 
 }
 
 
 protected void Button1_Click(object sender, EventArgs e)
 {
     RadGrid1.AllowPaging = false;
     RadGrid1.Rebind();
 
     if (Session["SelectedItems"] != null)
     {
         List<int> _SelectedItems = (List<int>)Session["SelectedItems"];
 
         foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
         {
             if (_SelectedItems.Where(i => i == Convert.ToInt32(item.GetDataKeyValue("ID"))).FirstOrDefault() == 0)
             {
                 item.Visible = false;
             }
             else
             {
                 item.Visible = true;
 
             }
         }
     }
 
 
     RadGrid1.ExportSettings.ExportOnlyData = true;
     RadGrid1.ExportSettings.OpenInNewWindow = true;
     RadGrid1.MasterTableView.ExportToExcel();
 
     RadGrid1.AllowPaging = false;
 
 
 }
 protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
 {
 
 }
 
 protected void AddORUpdateItem(int ID, bool IsAdd)
 {
     if (Session["SelectedItems"] == null)
     {
         Session["SelectedItems"] = new List<int>();
     }
 
     List<int> _SelectedItems = (List<int>)Session["SelectedItems"];
 
     if (IsAdd)
     {
         if (_SelectedItems.Where(i => i == ID).FirstOrDefault() == 0)
         {
             _SelectedItems.Add(ID);
         }
     }
     else
     {
         if (_SelectedItems.Where(i => i == ID).FirstOrDefault() != 0)
         {
             _SelectedItems.Remove(_SelectedItems.Where(i => i == ID).FirstOrDefault());
         }
     }
 
     Session["SelectedItems"] = _SelectedItems;
 }
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 {
     if (e.Item is GridDataItem)
     {
         if (Session["SelectedItems"] != null)
         {
             List<int> _SelectedItems = (List<int>)Session["SelectedItems"];
 
             GridDataItem item = e.Item as GridDataItem;
             int _Id = Convert.ToInt32(item.GetDataKeyValue("ID").ToString());
             if (_SelectedItems.Where(i => i == _Id).FirstOrDefault() != 0)
             {
                 item.Selected = true;
             }
             else
             {
                 item.Selected = false;
             }
         }
     }
 }
 
 
 
 protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
 {
     foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
     {
         int ID = Convert.ToInt32(item.GetDataKeyValue("ID"));
         if (item.Selected)
         {
             AddORUpdateItem(ID, true);
         }
         else
         {
             AddORUpdateItem(ID, false);
         }
     }
 }

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
 
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="True" AllowPaging="True"
    PageSize="2" AllowSorting="True" OnNeedDataSource="RadGrid1_NeedDataSource" CellSpacing="0"
    GridLines="None" ShowGroupPanel="True" OnItemCommand="RadGrid1_ItemCommand" OnItemDataBound="RadGrid1_ItemDataBound"
    AutoGenerateColumns="false" OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">
    <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true"></PagerStyle>
    <ClientSettings Selecting-AllowRowSelect="true" AllowDragToGroup="True">
        <Selecting AllowRowSelect="True"></Selecting>
    </ClientSettings>
    <MasterTableView DataKeyNames="ID">
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
        </Columns>
        <PagerStyle AlwaysVisible="True"></PagerStyle>
    </MasterTableView>
</telerik:RadGrid>
<br />
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" Width="130px"
    Height="25px" ValidationGroup="val" />
<script type="text/javascript">
           function onRequestStart(sender, args) {
               if (args.get_eventTarget().indexOf("Button1") >= 0) {
                   args.set_enableAjax(false);
               }
           }
       </script>



Thanks.
Jayesh Goyani
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or