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 3302264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:50:01+00:00 2026-05-17T20:50:01+00:00

I wanted to find the datagrid column header when a cell is clicked.. i

  • 0

I wanted to find the datagrid column header when a cell is clicked.. i used the following code

private void grid1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  {    
    DependencyObject dep = (DependencyObject)e.OriginalSource;
       while ((dep != null) &&     
            !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    if (dep is DataGridColumnHeader)
    {
        DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;

        if (columnHeader.ToString() == "Adv Comments")
        {
        MessageBox.Show(columnHeader.Column.Header.ToString());

        }
    }
    if (dep is DataGridCell)
        {
            DataGridCell cell = dep as DataGridCell;

        }
     }

But the column header is not a direct parent for the datagrid cell so its not able to find it. is there anyother way out??

  • 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-05-17T20:50:01+00:00Added an answer on May 17, 2026 at 8:50 pm

    The original source being clicked isn’t really connected to the so called item container (see the DataGrid.ItemContainerGenerator) so trying to work yourself up the hiearchy, although a nice idea won’t get you to far.

    For a quite silly simple solution you could use the knowledge of it being only one cell being clicked and thus using that clicked cell to retrieve the column, as this:

    private void DataGrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        // First check so that we´ve only got one clicked cell
        if(myGrid.SelectedCells.Count != 1)
            return;
    
        // Then fetch the column header
        string selectedColumnHeader = (string)myGrid.SelectedCells[0].Column.Header;
    }
    

    This perhaps ain´t the prettiest of solutions but simple is king.

    Hope it helps!

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

Sidebar

Related Questions

I am working on the following code and wanted to find the index of
Hey I just wanted to find out the working of the following script as
I am converting fortran code to C++ and wanted to find a right option
I am porting some Java code to C++ and wanted to find something that
I wanted to find the following function with etags: dt() but as I go
I wanted to find a way to upload a single file*, in the background,
I decided to learn concurrency and wanted to find out in how many ways
This is a common scenario, but i wanted to find out which way is
I'm not sure if this is possible at all, but wanted to find out
I know I could use PHP to do this, but wanted to find out

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.