locked
How to get the column header name when a cell is clicked in datagrid RRS feed

  • Question

    1.  hi

                i want to get the column header name of if clicked cell in datagrid i tried this code but it will not work

      private void attendancegrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if (attendancegrid.SelectedCells.Count != 1)
                    return;

                // Then fetch the column header
                string selectedColumnHeader = (string)attendancegrid.SelectedCells[0].Column.Header;
            }

    if anyone have any idea help me with some code.

    Thursday, October 24, 2013 7:36 AM

Answers

  • Hi,

    >>How to get the column header name when a cell is clicked in data grid?

    The Header object of the Data Grid Column is actually the visible header of that column, whatever you set it to be. Data Grid Column is not part of the Visual Tree so there is not direct way to access the Data Grid Column Header for it (we can't even be sure it exists yet). But you can do something like this to try and access it.

    I created a sample for this issue. Something looks like this,

    XAML Code:

    <Grid>
    	<DataGrid x:Name="dgBooks" AutoGenerateColumns="False" PreviewMouseLeftButtonUp="dgBooks_PreviewMouseLeftButtonUp">
    		<DataGrid.Columns>
    			<DataGridTextColumn Header="Name" Width="220" Binding="{Binding Name}" />
    			<DataGridTextColumn Header="Book Title" Width="220" Binding="{Binding BookTitle}" />
    		</DataGrid.Columns>
    	</DataGrid>
    </Grid>

    C# Code:

    public partial class MainWindow : Window
    {
    	public MainWindow()
    	{
    		InitializeComponent();
    		List<Book> books = new List<Book>() 
    		{
    			new Book(){Name = "Visual C#",BookTitle="C#"},
    			new Book(){Name = "Visual Basic.NET",BookTitle="VB.NET"},
    			new Book(){Name = "WPF",BookTitle="WPF"},
    		};
    		this.dgBooks.ItemsSource = books;
    	}
    
    	private void dgBooks_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    	{
    		//Get Current Selected Column
    		DataGridColumn column = dgBooks.CurrentColumn;
    
    		DataGridColumnHeader columnHeader = GetColumnHeaderFromColumn(column);
    
    		MessageBox.Show(columnHeader.Content.ToString());
    	}
    
    	private DataGridColumnHeader GetColumnHeaderFromColumn(DataGridColumn column)
    	{
    		// dataGrid is the name of your DataGrid. In this case Name="dgBooks"
    		List<DataGridColumnHeader> columnHeaders = GetVisualChildCollection<DataGridColumnHeader>(dgBooks);
    		foreach (DataGridColumnHeader columnHeader in columnHeaders)
    		{
    			if (columnHeader.Column == column)
    			{
    				return columnHeader;
    			}
    		}
    		return null;
    	}
    
    	public List<T> GetVisualChildCollection<T>(object parent) where T : Visual
    	{
    		List<T> visualCollection = new List<T>();
    		GetVisualChildCollection(parent as DependencyObject, visualCollection);
    		return visualCollection;
    	}
    
    	private void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
    	{
    		int count = VisualTreeHelper.GetChildrenCount(parent);
    		for (int i = 0; i < count; i++)
    		{
    			DependencyObject child = VisualTreeHelper.GetChild(parent, i);
    			if (child is T)
    			{
    				visualCollection.Add(child as T);
    			}
    			else if (child != null)
    			{
    				GetVisualChildCollection(child, visualCollection);
    			}
    		}
    	}
    }
    
    public class Book
    {
    	public string Name { get; set; }
    	public string BookTitle { get; set; }
    }

    The result is shown in the following figure,

    BTW, I uploaded the source code to SkyDrive. You can download from here.

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 25, 2013 2:47 AM