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

How to with RadGridView using SpreadStreamExport how to export selection item(s) only

11 Answers 100 Views
GridView
This is a migrated thread and some comments may be shown as answers.
herb
Top achievements
Rank 1
Veteran
Iron
herb asked on 07 Dec 2019, 05:06 PM

Switching from "ExportToXlsx" to "SpreadStreamExportFormat" but not clear on how to use the select item feature to export only items selected.

code I currently have: --

       void OnExportDataCommandExecute(RadGridView param)
        {
            string fileFormat = "xlsx";
            GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(param);
            spreadStreamExport.SheetName = "Sheet1";
            spreadStreamExport.ExportFormat = SpreadStreamExportFormat.Xlsx;
            var dialog = new SaveFileDialog()
            {
                DefaultExt = fileFormat,
                Filter = string.Format("(*.{0})|*.{1}", fileFormat, fileFormat)
            };
            if (dialog.ShowDialog() == true)
            {
                switch (fileFormat)
                {
                    case "xlsx":
                        spreadStreamExport.RunExport(dialog.FileName.ToString(),
                            new SpreadStreamExportRenderer(),
                            new GridViewSpreadStreamExportOptions()
                            {
        
        ShowColumnHeaders = true,
                                ShowColumnFooters = true,
                                ExportDefaultStyles = false,
                            });
                        break;
                    case "pdf":
                        //param.ExportToPdf(stream);
                        break;
                }
            }
        }

--

I see not method for include selected.item or such.

11 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 11 Dec 2019, 02:12 PM

Hello Herb,

To achieve the desired result, you can use the Items property of the GridViewSpreadStreamExportOptions and pass the SelectedItems collection to it.

                GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(this.playersGrid);
                spreadStreamExport.RunExport(dialog.FileName.ToString(),
                                             new SpreadStreamExportRenderer(),
                                             new GridViewSpreadStreamExportOptions()
                                             {
                                                 ShowColumnHeaders = true,
                                                 ShowColumnFooters = true,
                                                 ExportDefaultStyles = true,
                                                 Items = this.playersGrid.SelectedItems
                                             });

I've prepared a small sample project to demonstrate this in action. Please have a look and let me know if the same approach works for you.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 11 Dec 2019, 09:48 PM

Sorry does not allow me to specify "selectedItems". Here is some code I hope will show where i'm stuck. I it MVVM project.

From "MainWindow.xaml" calls the user control MRSUSERSETTING

<telerik:RadTabItem DropDownContent="MRS Users"
                                Header="MRS Users">
                <telerik:RadTabItem.Content>
                    <Grid>
                        <userControls:MrsUserSettings x:Name="MrsUserSettings"
                                                      Grid.Row="1"
                                                      Visibility="{Binding IsVisible, FallbackValue=Visible, Mode=TwoWay, TargetNullValue=Visible}"
                                                      Margin="0,0,0,0" />
                    </Grid>
                </telerik:RadTabItem.Content>
            </telerik:RadTabItem>
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 11 Dec 2019, 09:54 PM
This is the viewmodel
///Adding export and copy feature to MRS User tab/grid to build the user experience. <11/23/2019
///
 
using Microsoft.Win32;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Wpsi.VmaUtilities.Helpers;
using Wpsi.VmaUtilities.Models;
using System.Windows.Media;
using System.Threading;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView.SpreadsheetStreamingExport;
 
 
namespace Wpsi.VmaUtilities.ViewModels
{
    public class MrsUserSettingsViewModel: GalaSoft.MvvmLight.ViewModelBase
    {
        private DataTable _mrsUserDataTable;
        private string _smtpSearchString;
 
        //SQL
        private SqlConnectionStringBuilder m_SqlConnStrBuilder;
 
        #region RelayCommands
        public RelayCommand OkCommand
        {
            get;
            //{
            //    return new RelayCommand(Save);
            //}
        }
        public RelayCommand LoadMrsUsersCommand
        {
            get;
            //{
            //    return new RelayCommand(Save);
            //}
        }
        public RelayCommand<RadGridView> ExportDataCommand
        {
            get;
        }
 
        #endregion
 
        public MrsUserSettingsViewModel()
        {
            OkCommand = new RelayCommand(OnOkCommand);
            LoadMrsUsersCommand = new RelayCommand(OnLoadMrsUsersCommandExecute, () => true);
            ExportDataCommand = new RelayCommand<RadGridView>(rgv =>
            {
                OnExportDataCommandExecute(rgv);
            });
 
        }
 
        void OnExportDataCommandExecute(RadGridView param)
        {
            string fileFormat = "xlsx";
 
            GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(param);
            spreadStreamExport.SheetName = "Sheet1";
            spreadStreamExport.ExportFormat = SpreadStreamExportFormat.Xlsx;
 
            var dialog = new SaveFileDialog()
            {
                DefaultExt = fileFormat,
                Filter = string.Format("(*.{0})|*.{1}", fileFormat, fileFormat)
            };
 
            if (dialog.ShowDialog() == true)
            {
                switch (fileFormat)
                {
                    case "xlsx":
                        spreadStreamExport.RunExport(dialog.FileName.ToString(),
                            new SpreadStreamExportRenderer(),
                            new GridViewSpreadStreamExportOptions()
                            {
                                 
                                ShowColumnHeaders = true,
                                ShowColumnFooters = true,
                                ExportDefaultStyles = false,
                                 
                            });
                        break;
                    case "pdf":
                        //param.ExportToPdf(stream);
                        break;
                }
            }
        }
 
        private void OnLoadMrsUsersCommandExecute()
        {
            SQLMrsUser();
        }
 
        #region Properties
 
        public DataTable MrsUserDataTable
        {
            get => _mrsUserDataTable;
            set
            {
                if (_mrsUserDataTable == value)
                {
                    return;
                }
                _mrsUserDataTable = value;
                RaisePropertyChanged(nameof(MrsUserDataTable));//This name must match the name in the "ItemsSource="{Binding MrsUserDataTable}" of MrsUserSettings.xaml
                //for this to work.
            }
        }
 
        public string SmtpSearchString
        {
            get => _smtpSearchString;
            set
            {
                if (_smtpSearchString == value)
                {
                    return;
                }
                _smtpSearchString = value;
                RaisePropertyChanged(nameof(SmtpSearchString));
                //for this to work.
            }
        }
 
        #endregion
 
        #region Methods
        public bool CanOk()
        {
            return true;
        }
 
        public void OnOkCommand()
        {
            //Now Close the control
        }
 
        #endregion
        public void SQLMrsUser()
        {
            SqlCommand _sqlCommand = new SqlCommand();
            SqlCommand sqlcmd = null;
 
            //VmaUtilities.PostAppStatusMessageUpdateEvent("Loading...", Brushes.Red);
 
            m_SqlConnStrBuilder = SqlHelper.SetupSqlConnectionStringBuilder(VmaUtilities.AppSettings.SelectedSqlDatabaseProfile); //VmaUtilities.VmaUtils.SetupSqlConnectionInfo();
 
            sqlcmd = new SqlCommand(@"Select * FROM dbo.MRSUSERS");
            sqlcmd.CommandType = CommandType.Text;
 
            _sqlCommand = sqlcmd;
 
            try
            {
                //Now Lets Try to Connect
                using (SqlConnection m_SqlConnection = new SqlConnection(m_SqlConnStrBuilder.ConnectionString))
                {
                    m_SqlConnection.Open();
                    _sqlCommand.Connection = m_SqlConnection;
                    //Be sure we update the Status Bar with the connection string info.
                    VmaUtilities.PostUpdateDatabaseConnectionEvent(enConnectionStatus.Connected, "Successful Connection To Database", "Successful Connection To Database");
 
                    SqlDataAdapter dataAdapter = new SqlDataAdapter();
                    dataAdapter.SelectCommand = _sqlCommand;
                    DataTable dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);
                    MrsUserDataTable = dataTable;
                }
 
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine("Error Setting Connection to MRSUSERS, Error Message: {0}", ex.Message);
                throw;
            }
        }
    }
}
0
Dilyan Traykov
Telerik team
answered on 13 Dec 2019, 03:58 PM

Hello Herb,

Thank you for the provided code snippets.

Can you please specify why the SelectedItems collection is not accessible? Based on the code you provided, I believe you can accomplish the desired result using the following code snippet:

                    case "xlsx":
                        spreadStreamExport.RunExport(dialog.FileName.ToString(),
                            new SpreadStreamExportRenderer(),
                            new GridViewSpreadStreamExportOptions()
                            {
                                Items = param.SelectedItems,
                                ShowColumnHeaders = true,
                                ShowColumnFooters = true,
                                ExportDefaultStyles = false,

                            });
If this does not work for you, please provide more details on your exact setup so that I can better assist you. If possible, please send over a small sample project for me to investigate.

I will be looking forward to your reply.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 13 Dec 2019, 04:25 PM

Well I failed to try to use "param.SelectedItems".

It does work. However I'm surprise that the excel sheet shows a black background with white text. This because the RadGridView is way?

0
Dilyan Traykov
Telerik team
answered on 16 Dec 2019, 12:16 PM

Hello Herb,

The background and foreground of the cells in the sheet should only be changed if the ExportDefaultStyles property is set to true.

I tested this in the sample project I sent you earlier and if this setting is disabled, all cells are exported with a white background and black foreground.

Can you please try running the project, changing the ExportDefaultStyles setting and check the exported document? If you observe the same result as I described in the previous paragraph, please let me know how this differs from the setup you have at your end.

If possible, please provide a small sample project which demonstrates the undesired result. I will be looking forward to your reply.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 16 Dec 2019, 05:28 PM
Dilyan, First thank you for the sample code. I don't know why I did not try this earlier, but it won't run correctly due to the grid is not populating with PLAYER details so when it attempts to implement "GridViewSpreadStreamExport" it fails.
0
Dilyan Traykov
Telerik team
answered on 17 Dec 2019, 02:33 PM

Hello Herb,

Can you please clarify what you mean by "the grid is not populating with PLAYER details"? Also, how exactly does the export operation fail - does it throw an exception? One reason for this behavior that comes to mind is if the project has automatically resolved the Telerik references from the Binaries.NoXaml folder of your Telerik UI for WPF installation. You can read more about the difference between the Xaml and NoXaml assemblies here: Xaml vs. NoXaml.

If this is indeed the case, what I can suggest is to manually replace these references to point to the Binaries folder instead.

I've prepared a short recording where the export functionality works as expected at my end.

Please have a look and let me know whether this is indeed the desired result and whether replacing the type of binaries solves the issue.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 17 Dec 2019, 04:30 PM

Hello Dilyan,

Thank you for the clarification it is working. It had "binaries.nonxaml" path. Once switched it works.

I will be doing further testing today once few other matters are completed.

0
Accepted
Dilyan Traykov
Telerik team
answered on 18 Dec 2019, 09:31 AM

Hello Herb,

Please let me know once you're done with the testing. I will be awaiting your reply.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 18 Dec 2019, 06:19 PM

Thank you Dilyan.

I have to switch gears on this. However you have answered and provided a good example to revisit this.

Thanks.

Tags
GridView
Asked by
herb
Top achievements
Rank 1
Veteran
Iron
Answers by
Dilyan Traykov
Telerik team
herb
Top achievements
Rank 1
Veteran
Iron
Share this question
or