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

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

7 Answers 354 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 03 Dec 2012, 08:18 PM
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

7 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 04 Dec 2012, 04:19 PM
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.

0
Steve
Top achievements
Rank 1
answered on 04 Dec 2012, 04:58 PM
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
0
Dimitrina
Telerik team
answered on 07 Dec 2012, 09:31 AM
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.

0
Steve
Top achievements
Rank 1
answered on 10 Dec 2012, 08:57 PM
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;
    }
}




0
Vlad
Telerik team
answered on 11 Dec 2012, 08:20 AM
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.

0
Steve
Top achievements
Rank 1
answered on 11 Dec 2012, 05:19 PM
Vlad - Read my original post.
0
Steve
Top achievements
Rank 1
answered on 11 Dec 2012, 09:31 PM
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

Tags
GridView
Asked by
Steve
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Steve
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or