Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:33:32+00:00 2026-06-15T15:33:32+00:00

I have a WPF program that produces several DataGrids. I have added capability to

  • 0

I have a WPF program that produces several DataGrids. I have added capability to export each grid to Excel using the ideas at this site:
http://www.codeproject.com/Articles/120480/Export-to-Excel-Functionality-in-WPF-DataGrid

Currently I just have a button under each grid that has its own handler that looks something like this:

private void m_btnExportTimePartitionToExcel_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            ExportToExcel<SystemTimeRecordData, List<SystemTimeRecordData>> s = new ExportToExcel<SystemTimeRecordData, List<SystemTimeRecordData>>();
            ICollectionView view = CollectionViewSource.GetDefaultView(m_gridPartitionSystemTimeRecords.ItemsSource);
            s.dataToPrint = (List<SystemTimeRecordData>)view.SourceCollection;
            s.GenerateReport();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Problem with exporting Excel. Error: " + ex.Message);
        }
    }

I have a similar button handler for each grid. This all works, but it “smells”. It seems there should be a way to have just one handler that each button calls. That is, if I can pass the grid associated with that button, and the relevant class. Can I do that from XAML? I have searched and seen examples of passing arguments, but not the grid itself and certainly not the class, which I’m guessing would have to be passed as a Type?
It would be nice to replace the above code with something like…

private void m_btnExportTimePartitionToExcel_Click(object sender, RoutedEventArgs e)
    {
        try
        {
                                    // some how get Type type, and grid from sender and/or e
            ExportToExcel<type, List<type>> s = new ExportToExcel<type, List<type>>();
            ICollectionView view = CollectionViewSource.GetDefaultView(m_grid.ItemsSource);
            s.dataToPrint = (List<type>)view.SourceCollection;
            s.GenerateReport();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Problem with exporting Excel. Error: " + ex.Message);
        }
    }

Then I could just have one button handler! Any ideas?
Thanks,
Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:33:33+00:00Added an answer on June 15, 2026 at 3:33 pm

    Here are a few ways you could do that:

    • You could try passing the DataGrid as the CommandParameter if you can find it using an ElementName or RelativeSource binding

      <Button CommandParameter="{Binding ElementName=DataGridA}" ... />
      
      <!-- Will only work if this is inside the DataGrid somewhere, like in the footer -->
      <Button CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}" ... />
      

      You could then get the value with ((Button)sender).CommandParameter as DataGrid

    • You could pass the ItemsSource as a CommandParameter instead of the entire DataGrid

      <StackPanel>
          <DataGrid ItemsSource="{Binding SomeCollection}" ... />
          <Button CommandParameter="{Binding SomeCollection}" ... />
      </StackPanel>
      
    • You could navigate the VisualTree to find the closest DataGrid. I have some helpers on my blog that make this easy. Here’s an example using them with the above XAML:

      var parentPanel = VisualTreeHelpers.FindAncestor<StackPanel>((Button)sender);
      var datagrid = VisualTreeHelpers.FindChild<DataGrid>(parentPanel);
      

    I’m sure there’s other ways too, but those are the first ones I thought of 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c# program using WPF. I have it that when you press
In a C# WPF program I have a grid that I have successfully populated
The Situation I have a WPF program that I want to print several documents
I have a wpf program in c# that has a combobox with various colors
I have a C# WPF program using AciveX objects and providing WCF service. So
I have a WPF program that I'm developing in which I want to catch
I have a wpf application that has a button for for each folder under
I have a WPF program that runs fine on the development PC and on
I have a WPF Program that uses a Soap service that is generated by
I have created a program that subscribes to WPF's touch events. Everything has been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.