GridView - Getting the column datatype?

8 posts, 0 answers
  1. Adrian
    Adrian avatar
    11 posts
    Member since:
    Apr 2012

    Posted 22 Nov 2012 Link to this post

    Hello there, is it possible to programatically find out what the datatype is for a column in a RadGridView control?  By datatype I mean things like int, string, etc.
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Nov 2012 Link to this post

    Hello,

    You could find it through the DataType property of the bound column. Have you tried this?

    Greetings,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Adrian
    Adrian avatar
    11 posts
    Member since:
    Apr 2012

    Posted 22 Nov 2012 Link to this post

    I managed to do this and was just about to post it here when I noticed your post.  You are right about the DataType property, however I had to cast my column as a GridViewDataColumn in order to see it (it was not immediately obvious that I needed to do this, and the DataType property was not visible without the cast).
    ((Telerik.Windows.Controls.GridViewDataColumn)radGridView.Columns[0]).DataType

    Thanks anyway.
  5. Ben
    Ben avatar
    4 posts
    Member since:
    Jul 2015

    Posted 18 Aug 2015 in reply to Dimitrina Link to this post

    Is it possible to set a custom attribute on my data, and read that same attribute from the DataType property in my OnAutoGeneratingColumn event handler?

     

    My initial tests show that the name and type are correct, but I see no attributes that I had added.

  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Aug 2015 Link to this post

    Hi,

    How have you set a custom attribute? Basically, RadGridView respects Data Annotations and you can try this. You can check the following msdn article:
    Using Data Annotations to Customize Data Classes.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Ben
    Ben avatar
    4 posts
    Member since:
    Jul 2015

    Posted 19 Aug 2015 in reply to Dimitrina Link to this post

    First off, I apologize, this was meant for the WPF forum, but I posted too quickly. Please move it if you need to.

    I am using some of the DataAnnotations attributes, and they work wonderfully. My hope was to set which properties would aggregate with a format string with a custom attribute. Something like below. 

    [Display(Name = "Trade Amount")]
    [DisplayFormat(DataFormatString = "{0:C}")]
    [DisplayAggregate(AggregateFunction.Sum, ResultFormatString = "Total: {0:C}")]
    [DisplayAggregate(AggregateFunction.Min, ResultFormatString = "Min: {0:C}")]
    public double Amount { get; set; }

     I actually got this working after posting, but it is a bit of a workaround. In the AutoGeneratingColumn event, I match the column name against the RadGridView item and pull the attributes from there, not the column.datatype. But if there is a more elegant solution, I wouldn't mind changing my existing work. 
     Cheers.

    private void ResultsGridView_OnAutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        var column = e.Column as GridViewDataColumn;
        if (column == null || ResultsGridView.Items.Count == 0) return;
        var propName = column.DataMemberBinding.Path.Path;
        var props = ResultsGridView.Items[0].GetType().GetProperties();
        var aggregates = (from prop in props where prop.Name == propName select (DisplayAggregate[]) prop.GetCustomAttributes(typeof (DisplayAggregate), false)).FirstOrDefault();
        AddAggregatesToGridColumn(aggregates, column);
    }

  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Aug 2015 Link to this post

    Hello,

    As it turns out there is not a more elegant way to propose.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Ben
    Ben avatar
    4 posts
    Member since:
    Jul 2015

    Posted 20 Aug 2015 in reply to Dimitrina Link to this post

    Fair enough. Thank you for your time.
Back to Top
DevCraft banner