Change Background Color in C1.WPF.C!Datagrid

Posted by: christoph.schattka on 3 December 2021, 10:06 am EST

    • Post Options:
    • Link

    Posted 3 December 2021, 10:06 am EST - Updated 4 October 2022, 8:36 am EST

    Hello,

    I’m using an old C1Datagrid for WPF and now I would like to change the background color, if a special character in the cell was detected. Attached you will find some basic information of the grid. I’m trying to change the background color during runtime:

    int_k = DG_Vorauswahl.Rows.Count

            For int_i = 0 To int_k - 1
    
                If DG_Vorauswahl.Columns("Stillstand").Caption = "Stillstand" Then
    
                    If DG_Vorauswahl.Rows.Item(int_i).Cells("Stillstand").Value = "J" Then
                       DG_Vorauswahl.Rows.Item(int_i).Cells("Stillstand").SetValue(BackgroundProperty, Brushes.Red)
    
                        int_i = int_i + 1
    
                    End If
    
                End If
    
            Next
    

    But this solution doesn’t work. Any advice?

    Many Thanks for your help.

    Greetings Chris

  • Posted 3 December 2021, 11:41 am EST

    Hello,

    maybe i wrote too quick and so my first post ever isn’t understandable. To clarify: I would like to change the CELL background color, if the special character was found. Sorry again and many thanks fpr your help.

    Best regards

    Chris

  • Posted 6 December 2021, 2:29 am EST

    Hi Chris,

    You can change the background of the cells containing special character by using DataTrigger with C1DataGrid’s ItemCellTemplate.

    For example, you can change the background for the cells containing ‘@’ character as follows:

    
     <c1grid:C1DataGrid.ItemCellTemplate>
                <ControlTemplate>
                    <TextBlock Text="{Binding Value}">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Value, Converter={StaticResource converter}, 
                                        ConverterParameter=@}" Value="True">
                                        <Setter Property="Background" Value="Orange"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </ControlTemplate>
    </c1grid:C1DataGrid.ItemCellTemplate>
    
    

    Please refer to the attached sample for full implementation. (see DataGridCellBackground.zip)

    Best Regards,

    Kartik

    DataGridCellBackground.zip

  • Posted 7 December 2021, 11:39 am EST - Updated 4 October 2022, 8:36 am EST

    Hello Kartik,

    first: many thanks for your support and your solution. Your example works fine and i understand how its working.

    Today i try to implement it to my project and only one point is open.

    How to hand over my datatable after an sql query to your observableCollection

    ----my vb Code

    Imports System.Data

    Imports System.Data.OleDb

    Imports System.Collections.ObjectModel

    Imports System.Globalization

    Namespace NS_Störungsübersicht

    Partial Public Class C1WPF_Störungsübersicht

    Inherits Window

        Public datas As ObservableCollection(Of Data) = New ObservableCollection(Of Data)()
    

    ----get the data from my database

    Private Sub DB_Query()

            Dim dt As New System.Data.DataTable
    
            Try
    
                connection.Open()
    
                cmd = New OleDbCommand("Select edd.param01 as Zugehörig, edd.verweis as Nr, er.erfass_dat As Angelegt, rm.bez_lang As Störungskatalog, Substring(edd.dlg_data,11,490) As Meldung, Concat(p.person_vorname, ' ',p.person_name) as Melder, edd.param_str01 as Status, er.grund_19 as Stillstand, er.grund_20 as Bis FROM event_res er, event_dlg_data edd, personen p, res_massnahmen rm where er.artikel = (Select rb.res_nr from res_bestand rb where rb.bezeichnung='" + cmb_Ressource.Text + "') and edd.verweis=er.dlg_data_verweis and er.person_nr=p.karten_nummer and rm.res_familie = (Select rb.res_familie from res_bestand rb where rb.bezeichnung='" + cmb_Ressource.Text + "' and rm.massnahme = er.info_03 ) and edd.param_d01 = '1' ORDER BY erfass_dat DESC", connection)
    
                dt.Load(cmd.ExecuteReader)
    

    ----and now i should hand over the data for the converter before my code goes ahead to fill the C1Datagrid

                DG_Vorauswahl.ItemsSource = dt.AsDataView
    
                connection.Close()
    

    Hope I will find the solution cause the actual result looks like this:

    Thanks again and I hope. I can post you the final result :-).

    Best Regards,

    Chris

  • Posted 8 December 2021, 1:29 am EST

    Hi,

    Please note, we can bind our C1DataGrid to any collection and DataTable as well.

    Please check the attached sample below.

    If you want to convert your DataTable to an observable collection then you have to create an ObservableCollection traversing each row of DataTable.

    
    var _testCollection = new ObservableCollection<Test>();
    foreach(var row in TestTable.Rows)
    {
        var obj = new Test()
        {
            Id = (int)row["id"],
            Name = (string)row["name"],
    	.....
        };
        _testCollection.Add(obj);
    }
    
    public class Test
    {
    public int Id { get; set; }
    public string Name { get; set; }
    }
    
    

    Also, you can find many articles on internet/google searching with “How to change DataTable to ObservableCollection”.

    Hope, it will solve you issue.

    Thanks,

    Sonu Singh

    DataGridCellBackground.zip

  • Posted 9 December 2021, 8:05 am EST - Updated 4 October 2022, 8:36 am EST

    Hello Sonu,

    thank you for your support. Now is’t working fine.

    I found the reason for the debugging / runtime issue. Your example contains a “full” set of data. In my real database of course some values can be missing.

    So the problem was the converter. I add a small if-query to catch DBNull or ISNothing values. From this moment it works.

    Namespace NS_Störungsübersicht

    Friend Class SpecialCharacterConverter
        Implements IValueConverter
    
        Dim str_help As String = ""
    
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    

    —> additonal If value Is DBNull.Value Or IsNothing(value) Then

    str_help = “”

    Else

    str_help = value.ToString

    ----> End If

            If String.IsNullOrEmpty(str_help) Then Return False
            Return str_help.Contains(parameter.ToString())
    
        End Function
    
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    
    End Class
    

    End Namespace


    Best Regards,

    Chris

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels