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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

SFDataGrid iterate through Cells

Thread ID:

Created:

Updated:

Platform:

Replies:

126273 Oct 5,2016 08:57 AM UTC Oct 12,2016 06:55 PM UTC Xamarin.Forms 5
loading
Tags: SfDataGrid
Thomas Gruener
Asked On October 5, 2016 08:57 AM UTC

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?

Ashok N [Syncfusion]
Replied On 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   


Thomas Gruener
Replied On 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

Divakar Subramaniam [Syncfusion]
Replied On 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.   


Thomas Gruener
Replied On 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

Ashok N [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;