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

Export selection to excel in a MVVM WPF program

3 Answers 460 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 20 Nov 2019, 08:07 PM

I'm currenting working on an existing project where there the creator had UserControl Xamls, the controls are in ViewModel folder where usercontrol.ViewModel.cs has the control and executable.

I'm trying not to break the code, learn how to use MVVM/WPF formatting.

I have added a button to the UserControl.Xaml now I'm not clear on get the button control to work so it can export both selected gridview item or the whole gridview. I have found several forum articles but they do not met my WPF format:

<telerik:RadButton x:Name="btnExport"
                                   Grid.Row="0"
                                   Content="Export to Excel"
                                   Command="{Binding Export2ExcelCommand}"
                                   Width="100"
                                   Margin="4" />

But now I need a good sample that show how I should add the "Export2ExcelCommand" button control to the "ucViewModel.cs" file.

Again the goal is to support both whole gridview export or selected items in gridview to Excel.

Help is appreciated.

3 Answers, 1 is accepted

Sort by
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 21 Nov 2019, 02:46 PM

here is the code use to populate the RadGridView:

public void SQLMrsUser()
        {
            SqlCommand _sqlCommand = new SqlCommand();
            SqlCommand sqlcmd = null;
            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
Accepted
Vladimir Stoyanov
Telerik team
answered on 25 Nov 2019, 12:51 PM

Hello Herb,

You can check out the Exporting to Xlsx and Pdf RadGridView example from our demos. It demonstrates a possible approach for exporting the control with a command. 

You can also check out the ExportToXlsx method accepting a GridViewDocumentExportOptions as a parameter. This object has an Items property, where you can specify the items that need to be exported. You can add the selected item to the Items collection and it will be the only exported item. 

I hope you find this helpful.

Regards,
Vladimir Stoyanov
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 05 Dec 2019, 05:03 AM
Those helped.
Tags
GridView
Asked by
herb
Top achievements
Rank 1
Veteran
Iron
Answers by
herb
Top achievements
Rank 1
Veteran
Iron
Vladimir Stoyanov
Telerik team
Share this question
or