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

SFDataGrid iterate through Cells

How can I iterate through the Cells of a SFDataGrid for each Column and Row?
SInce there is no DataGrid.Items how I can get to them?

5 Replies

AN Ashok N Syncfusion Team October 6, 2016 07:03 PM UTC

Hi Thomas,   
  
Thanks for contacting Syncfusion support.   

 We have checked your query and you can get the cell values using the below code snippet 
private void button_Clicked(object sender, EventArgs e) 
{ 
    foreach (RecordEntry record in dataGrid.View.Records) 
    { 
        foreach (var column in dataGrid.Columns) 
        { 
            var cellValue = dataGrid.GetCellValue(record.Data, column.MappingName); 
        } 
    } 
} 
  
Regards,   
Ashok   



TG Thomas Gruener October 7, 2016 07:11 AM UTC

OK, now I have the CellValue, but how can I apply now some style on the Cell now?

foreach(var column gc in dataGrid.Columns)
{
           var cellvalue = dataGrid.GetCellValue(record.Data, column.MappingName);
           if(cellvalue == "test")
           {
                cell = ????
                cell.Style = new Style(GridCell,BackgroundColor, "Red");  ????   something like that
            }
}

Thanks


DS Divakar Subramaniam Syncfusion Team October 10, 2016 06:16 AM UTC

Hi Thomas,  
  
  
In SfDataGrid, you can able to set a style for the particular cell using GridColumn.CellStyle property and Converter. In the Converter, you will get the value of each cell in the column which you have bound to the converter. Based on the cell value you can apply different styles to each cell. Please refer the below code snippet to know how to apply the style to the particular cell using Converter.  
  
  
//Main Page  
Style style = new Style(typeof(GridCell));  
style.Setters.Add(new Setter() { Property = GridCell.BackgroundColorProperty, Value = newBinding() { Path = "OrderID"Converter = new Converter() } });  
  
foreach (var column in sfGrid.Columns)  
{  
    if (column.MappingName == "OrderID")  
        column.CellStyle = style;  
}  
  
//Converter class  
public class Converter : IValueConverter  
{  
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
    {  
        var data = (int)value;  
        if (data == 10004)  
            return Color.Blue; // You can achieve your requirement here.(applying styles for particular cell based on the cell value)  
        return Color.Default;  
    }  
  
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
    {  
        return null;  
    }  
}  
  
  
In the above code, we have bound “OrderID” property. So, you can get each value of the “OrderID” column. Hence, you can apply a style to any cell based on the cell value as above. We have applied background color for the cell having the value as 10004 in the above code. You can modify the code based on your requirement and can apply different styles to the particular cell.  
  
  
  
Regards,  
Divakar.   



TG Thomas Gruener October 11, 2016 02:24 PM UTC

Hi,
this won't work for me, because I need to highlight all the lowest and all the highest values of a column.
I created some work around which should normally work, but for some reason it doesn't apply the styles.
Please check the files in the Attachment. Just copy paste them into the PCL part of the Project to test them.
The code iterates through all items and columns, and keeps the row and item number stored in an Array.
Than it makes a second iteration through it to set the Styles.

Please let me know if you can find the Problem why this doesn't work as it should????

Attachment: ConverterDemo_11f2ef40.zip


AN Ashok N Syncfusion Team October 12, 2016 06:55 PM UTC

Hi Thomas,  

We have checked your sample. In the sample, you have set the style for the cells in SfDataGrid.GridLoaded event. Before this event triggered, SfDataGrid will be initialized. That’s why the converter in your sample is not worked. However, you can overcome this problem by applying the styles in SfDataGrid.GridViewCreated event as in below code snippet.  
  
sfGrid.GridViewCreated += SfGrid_GridViewCreated;  
  
private void SfGrid_GridViewCreated(object sender, GridViewCreatedEventArgs e)  
{  
    SfDataGrid dg = (SfDataGrid)sender;  
    //get the max items and min items from Grid for each column  
    List<int[]> maxList = GetColumnValue(dg, true);  
    List<int[]> lowList = GetColumnValue(dg, false);  
  
    //highlight the cells  
    HighLightCells(dg, lowList, true);  
    HighLightCells(dg, maxList, false);  
}  
  
We have modified your sample by resolved your problem and you can download the same from the below link.  
  
  
Regards,  
Ashok 


Loader.
Up arrow icon