change row colour dynamicaly

     <GroupBox x:Name="grp_grdPortaria"  Background="PapayaWhip" Margin="0,65,0,5" Visibility="Visible">
                <syncfusion:SfDataGrid x:Name="grdPortaria" Margin="0,0,0,0" VerticalAlignment="Top"
                                       ColumnSizer="Auto" AllowTriStateSorting="False" ShowSortNumbers="False" ShowGroupDropArea="False" AllowFiltering="True"
                                       AutoGenerateColumns="False" AllowResizingColumns="True" FontSize="14" AllowSorting="True"  >
                    <syncfusion:SfDataGrid.Columns>
                        <syncfusion:GridTextColumn HeaderText="Unid" MappingName="COD" IsReadOnly="True" Width="50"  />
                        <syncfusion:GridTextColumn HeaderText="Morador" MappingName="Nome" IsReadOnly="True" Width="250"  />
                    </syncfusion:SfDataGrid.Columns>
                </syncfusion:SfDataGrid>
            </GroupBox>
My CS i have a function to populate.. and to create my columns .. 
            string queryString = "", cCOD = "";

            Boolean lJaTem = false;
            String cProcura = txtProcura.Text.Trim().Replace("'", "").Replace("select", "").Replace("exec", "").Replace("update", "").Replace("drop", "");
            if (Convert.ToInt32(Variaveis.IniDados.idCondUnidade) > 0)
            {
                cCOD = Variaveis.IniDados.COD;
            }
// here I get from the DB the query string to create my browse.. 
            queryString = "execute sp_portaria '" + myFuncoes.dtNow().ToString("yyyy-MM-dd HH:mm:ss.fff") + "' ," + Variaveis.IniDados.idCondominio + ",'" + cCOD + "','S'";
            DataTable ddlDT = myFuncoes.db_select(queryString);
            queryString = ddlDT.Rows[0][0].ToString();

/// I set the filter in the query
            queryString = queryString.Replace("@idC", Variaveis.IniDados.idCondominio.ToString());
            queryString = queryString.Replace("@Pes", "'" + cProcura + "'");

// here I hage the datatable
            DataTable dtScreen = myFuncoes.db_select(queryString);

// I discovered this way to set height if I have text wrapping on cells..
            grdPortaria.QueryRowHeight += dataGrid_QueryRowHeight;

// Here I create the columns on the grid.. 
// How can I bind and activete the function to set background colour AT THE BOTTOM OF THIS TEXT.. ..??
            if (dtScreen.Columns.IndexOf("ctpMor") > 0
                && !temColuna("ctpMor"))
            {
                grdPortaria.Columns.Add(new GridTextColumn()
                {
                    HeaderText = "Class",
                    MappingName = "ctpMor",
                    Width = 120,
                    ValueBinding = new Binding()
                    {
                        Path = new PropertyPath("ctpMor"),
                        Converter = new ConvClass("ctpMor")
                    },
                    TextAlignment = TextAlignment.Left
                });
            }

this convclass works OK for data changing .. 
how can I do the same call on creating columns to change backgorundcolor in this function below ?

public class ColorConverter : IValueConverter
        {
            public ColorConverter(string grdColName)
            {
                ColName = grdColName;
            }
            public string ColName { get; }

            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null || value.GetType().FullName.ToLower().Contains("system.dbnull"))
                {
                    return "";
                }

                String cTexto = value.ToString();


                if (ColName == "ctpMor")
                {
                    if (cTexto.Contains("(MINQ)"))
                    {
                        return new SolidColorBrush(Colors.Tomato);
                    }
                    if (cTexto.Contains("(MPROP)"))
                    {
                        return new SolidColorBrush(Colors.LightBlue);
                    }
                }

                return DependencyProperty.UnsetValue;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }


1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team January 11, 2021 05:19 PM UTC

Hi Jorge, 
Thanks for contacting Syncfusion support.       
Based on provided information Dynamically change the color based on click into check column in SfDataGrid. Please refer the below code snippet for your reference, 
Code snippet XAML:       
<Window.Resources>    
    <local:CustomRowStyleConverter x:Key="CustomRowStyleConverter" />    
    <local:AlternativeRowStyleConverter x:Key="AlternativeRowStyleConverter" />    
    <Style x:Key="rowStyle" TargetType="Syncfusion:VirtualizingCellsControl">    
        <Setter Property="Background" Value="{Binding Converter={StaticResource CustomRowStyleConverter}, UpdateSourceTrigger=PropertyChanged}" />    
    </Style>    
    <Style x:Key="alternativeRowStyle" TargetType="Syncfusion:VirtualizingCellsControl">    
        <Setter Property="Background" Value="{Binding Converter={StaticResource AlternativeRowStyleConverter},UpdateSourceTrigger=PropertyChanged}"/>    
    </Style>    
</Window.Resources>    
    
<Syncfusion:SfDataGrid x:Name="datagrid"    
                       AllowEditing="True"    
                       ItemsSource="{Binding ItemsCollection}"    
                       RowStyle="{StaticResource rowStyle}"    
                       AlternatingRowStyle="{StaticResource alternativeRowStyle}" />    
   
   
C# Code snippet:       
public class CustomRowStyleConverter : IValueConverter    
{    
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)    
    {    
        if (value == null)    
            return DependencyProperty.UnsetValue;    
    
        if ((value as BusinessObjects).IsChecked)    
            return new SolidColorBrush(Colors.Green) { Opacity = 0.7 };    
        return new SolidColorBrush(Colors.AliceBlue);    
    }    
    
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultur    
    {    
        throw new NotImplementedException();    
    }    
}    
    
public class AlternativeRowStyleConverter : IValueConverter    
{    
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)    
    {    
        if (value == null)    
            return DependencyProperty.UnsetValue;    
    
        if ((value as BusinessObjects).IsChecked)    
            return new SolidColorBrush(Colors.Green) { Opacity = 0.7 };    
        return new SolidColorBrush(Colors.Beige);    
    }    
    
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultur    
    {    
        throw new NotImplementedException();    
    }    
}    
     
Sample link:  https://www.syncfusion.com/downloads/support/forum/161334/ze/ManualRefresh1120087948

If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon