How to get GridViewColumn Header Text when GridView uses DisplayAttribute.ShortName

8 posts, 0 answers
  1. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 03 Dec 2012 Link to this post

    The problem I am having is that I am creating a ContextMenu on the GridViewHeaderRow where I list the column header text with checkboxes so the user can select which ones to display.  When I assign the column.Header with text directly, obviously all I have to do is loop through and get column.Header; however when I don't assign the Header, and let the GridView figure the text by using the DisplayAttribute.ShortName of the bound data members, the column.Header is null.  I know the text must be somewhere, but I can't find it.

    Is there some way to determine the ShortName from the GridViewColumn in this scenario?

    Thanks in advance,
    Steve
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 04 Dec 2012 Link to this post

    Hi Steve,

    As I understand you would like to show a header ContextMenu with an option to select the columns to be displayed. If this is the case, then I would suggest you to check the "Header Context Menu" WPF demo.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 04 Dec 2012 Link to this post

    Hi Didie,  As a matter of fact that is the code I am using.  If you read my post more carefully, you'll see that my problem involves getting the header text when the Header is not explicitly assigned and is created by the GridView using the DisplayAttribute.ShortName assigned to the bound data member.  In this case when the following code executes the column.Header is null.
     
    .
    RadMenuItem subMenu = new RadMenuItem();
      subMenu.Header = column.Header;
      subMenu.IsCheckable = true;
      subMenu.IsChecked = true;

    Please reread my original post and advise.

    Thanks in advance,
    Steve
  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 07 Dec 2012 Link to this post

    Hello Steve,

    Have you tried to access the UniqueName of the Column instead? I believe it should have the correct text.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 10 Dec 2012 Link to this post

    I tried using the UniqueName field; however as you can see from the attached image and the following class definition, UniqueName contains the actual property name and not the DisplayAttribute.ShortName, whereas the column headers use the ShortName.  Perhaps there is some other method I could use to access the correct text.
    Thanks in advance,
    Steve

    public class Person
    {
        [Display(ShortName = "First Name")]
        public String FirstName
        {
            get;
            set;
        }
        [Display(ShortName = "Last Name")]
        public String LastName
        {
            get;
            set;
        }
     
        [Display(AutoGenerateFilter = false, AutoGenerateField = false)]
        public String FullName
        {
            get;
            set;
        }
     
        [Display(ShortName = "Email")]
        public String Email
        {
            get;
            set;
        }
        [Display(ShortName = "Employed by AccountMate")]
        public bool Employee
        {
            get;
            set;
        }
        [Display(ShortName = "Last Contact")]
        public DateTime LastContact
        {
            get;
            set;
        }
     
        public Person(String fn, String ln, String em, bool empl, DateTime lc)
        {
            FirstName = fn;
            LastName = ln;
            Email = em;
            Employee = empl;
            LastContact = lc;
        }
    }




  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 11 Dec 2012 Link to this post

    Hi,

    Have you tried Header property of the column?

    Kind regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 11 Dec 2012 Link to this post

    Vlad - Read my original post.
  9. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 11 Dec 2012 Link to this post

    I figured out what the problem is.  The version of GridViewHeaderMenu.cs that I was using initialized the menu on the grid.RowLoaded event
    private void RowLoaded(object sender, RowLoadedEventArgs e)
    {
        if (e.Row is GridViewHeaderRow)
        {
            InitializeMenus((GridViewHeaderRow) e.Row);
        }
    }

    The problem with this implementation is that the grid is not completely initialized at that point.  As a matter of fact if AutoGenerateColumns is true, the grid.Columns collection is empty when the RowLoaded event is fired.  By switching to the grid.Loaded event, all is good. 

    Thanks for your help,
    Steve

Back to Top
UI for WPF is Visual Studio 2017 Ready