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
close icon

WPF GridControl- How to fill the datasource with custom class collection

Guys we have a custom class which has enumeration of both rows and column(have implemented IEnumerator). This is similar to DataTable.
Can  we assign rows and columns of this custom class directly to GridControl or sfDataGrid rows and columns.

Currently we are using the same with Sync fusion grid in Winforms.  We have hooked up to the QueryCellInfo event of the gird. We will manually update each cell values only for the visible rows. While scrolling the grid, we will get the index of the first visible row in the QueryCellInfo, then we will update the values. Sorting, filtering etc are handled by our data structure. If the same could be done using WPF GridControl or sfDataGrid, that is also fine for us


I have attached our Table format in a very simple format .

Attachment: CustomTable_75a14b5.zip

7 Replies

AA Arulraj A Syncfusion Team March 5, 2019 10:24 AM UTC

Hi Hemanath, 

Thanks for using Syncfusion product. 

To populate the data using custom class, you could use the QueryCellInfo event. Please refer the following code example. 

C#                                                                                                                                                                                                                                   
gridControl.QueryCellInfo += GridControl_QueryCellInfo; 
 
 
private void GridControl_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) 
{ 
    if (e.Cell.RowIndex > 0 && e.Cell.ColumnIndex > 0) 
    { 
        if (dataSource.Count > e.Cell.RowIndex) 
            e.Style.CellValue = this.GetCellValue(dataSource[e.Cell.RowIndex - 1], e.Cell.ColumnIndex - 1); 
    } 
    else if (e.Cell.RowIndex == 0 && e.Cell.ColumnIndex > 0) 
    { 
        if (dataSource.Count > e.Cell.RowIndex) 
            e.Style.CellValue = this.GetColumnHeader(dataSource[0], e.Cell.ColumnIndex - 1); 
    } 
} 
 
private object GetCellValue(object data, int colIndex) 
{ 
    var properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.Instance); 
    if (properties.Count() > colIndex) 
    { 
        var value = properties[colIndex].GetValue(data, null); 
        return value; 
    } 
    return null; 
} 
 
private string GetColumnHeader(object data, int colIndex) 
{ 
    var properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.Instance); 
    if (properties.Count() > colIndex) 
    { 
        return properties[colIndex].Name; 
    } 
    return string.Empty; 
} 
 
 
If you want to save the edited cell value in underlying datasource, you could use the SaveCellFormattedText event. Please refer the following code example. 
C# 
gridControl.SaveCellFormattedText += GridControl_SaveCellFormattedText; 
 
private void GridControl_SaveCellFormattedText(object sender, GridCellTextEventArgs e) 
{ 
   if (e.Style.RowIndex > 0 && e.Style.ColumnIndex > 0) 
    { 
        if (dataSource.Count > e.Style.RowIndex) 
            this.SetCellValue(dataSource[e.Style.RowIndex - 1], e.Text, e.Style.ColumnIndex - 1); 
    } 
} 
 
private void SetCellValue(object data,object value, int colIndex) 
{ 
    var properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.Instance); 
    if (properties.Count() > colIndex) 
    { 
       properties[colIndex].SetValue(data, value, null); 
    } 
} 
 
 
Please get back us if you need any further assistance on this. 
 
Regards, 
Arulraj A 



HR Hemanath Ravi March 5, 2019 10:27 AM UTC

The code you shared is for WPF right?


AA Arulraj A Syncfusion Team March 5, 2019 10:30 AM UTC

Hi Hemanath, 

Thanks for the update. 

Yes, the provided code and sample is in WPF GridControl. Let us know if you need any further details. 

Regards, 
Arulraj A 



HR Hemanath Ravi March 21, 2019 06:23 AM UTC

Can I create hierarchical table for each row using Grid Control using QueryCellInfo approach. 
And also can I add custom buttons to Column Header like filter button. I have attached a screenshot for the same.

Attachment: Grid_search_142351be.zip


AA Arulraj A Syncfusion Team March 21, 2019 11:53 AM UTC

Hi Hemanath, 

Thanks for your update.  

If you need to implement grouping and filtering at control level, it is better to use the SfDataGrid control instead of GridControl. Please refer the following UG links to know more details about SfDataGrid. 


Please get back to us if you need any further assistance on this.  

Arulraj A 



HR Hemanath Ravi March 21, 2019 11:55 AM UTC

Could I use QueryCellInfo with SfDataGrid to populate data?


AA Arulraj A Syncfusion Team March 22, 2019 10:31 AM UTC

Hi Hemanath, 

Thanks for the update. 

No, you cannot populate the data using QueryCellInfo event in SfDataGrid, since it is not build on cell oriented. Since SfDataGrid is data oriented control you cannot customize cell based rather it supports only column wise. Please refer the following UG link to bind data in SfDataGrid. 
 
Arulraj A 


Loader.
Live Chat Icon For mobile
Up arrow icon