We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Data Annotation - Fill Datagrid

Is there a way to use "Data Annotation - Properties" to populate the DataGridView without having to set column name?
Ex:
[Column("First Name")]
public string FirstName { get; set; }
[Column("Last Name")]
public string LastName { get; set; }



3 Replies

MG Mohanraj Gunasekaran Syncfusion Team August 15, 2017 07:45 AM UTC

Hi Rodrigo, 

Thanks for Syncfusion product. 

In GridDataBoundGrid can populate the columns automatically based on the bounded collection underlying datasource but does not have the direct support for Data Annotation. But, you can achieve your scenario by using HeaderText property. Please refer to the below code example 
 
Code example 
Using System.ComponentModel.DataAnnotations; 
 
[Display(Name = "Category Name")] 
public string CategoryName 
{ 
                get 
                { 
                                return this.cat_Name; 
                } 
                set 
                { 
                                this.cat_Name = value; 
                } 
} 
 
Data d = new Data(); 
foreach (GridBoundColumn column in columns) 
{ 
    var col = (d.GetType().GetProperty(column.MappingName)).GetCustomAttributes(typeof(DisplayAttribute), true); 
    if (col is DisplayAttribute[] && (col as DisplayAttribute[]).Count() > 0) 
    { 
        string headerText = (col as DisplayAttribute[])[0].Name; 
        column.HeaderText = headerText; 
    } 
} 
 
 
Sample link: GridDataBoundGrid 
 
Note:  
Moreover, you can use GridGroupingControl instead of GridDataboundGridGridGroupingControl has more features and more flexibility compared to GridDataBoundGrid. 
 
 
Regards, 
Mohanraj G 
 



RF Rodrigo Ferro Siqueira August 15, 2017 05:17 PM UTC

Thanks... it Works =)



AR Arulpriya Ramalingam Syncfusion Team August 16, 2017 04:18 AM UTC

Hi Rodrigo,    
 
We are glad to hear that the provided solution was resolved your scenario.   
 
Please let us know if you need further assistance.    
   
Regards,   
Arulpriya 


Loader.
Up arrow icon