Flexgrid and cellStyle

Posted by: saidnai on 16 March 2022, 12:45 pm EST

    • Post Options:
    • Link

    Posted 16 March 2022, 12:45 pm EST

    Dear all,

    I use both functions below and they are working fine. Unlikely one cellStyle is overriding the other cellStyle. Is it possible to combine them in one cellStyle function so that the color can be set and at the same time the text can wrapped.

    here is the code:

    Function WrapCell()

    Try

    Dim cellStyleWrapped As CellStyle = Me.DG.Styles.Add(“Wrapped”, Me.DG.Styles.Normal)

    cellStyleWrapped.WordWrap = True

        Dim rw As Integer = 0
        For Each r As Row In DG.Rows
            If rw = 0 Then
            Else
                'Me.DG(rw, 2) = "Long text which should wrap"
                Me.DG.SetCellStyle(rw, 2, cellStyleWrapped)
                Me.DG.AutoSizeRow(rw)
            End If
            rw += 1
        Next
    
    Catch ex As Exception
        MsgBox("Error 'WrapCell' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
    Finally
        'Cursor.Current = Cursors.Default
    End Try
    

    End Function

    Private Function AssignForeColour() As Color

    Try

    For eRow As Integer = 1 To DG.Rows.Count - 1

    'ForeColor

    Dim csBlue As CellStyle = DG.Styles.Add(“csBlue”)

    csBlue.ForeColor = Color.Blue

    DG.Rows(eRow).Style = DG.Styles(“csBlue”)

    DG.SetCellStyle(eRow, 2, csBlue)

    Next

    Catch ex As Exception
        MsgBox("Error 'AssignForeColour' :" & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
    End Try
    

    End Function

    Best regards

    Said

  • Posted 17 March 2022, 4:03 am EST

    Hi Said,

    you would have to create a third CellStyle to combine settings from both styles.

    An easier approach might be to use the “C1FlexGridStyleHandler” helper class that I posted at https://github.com/WolfgangHG/C1FlexGridEnhancements/tree/master/C1FlexGrid452StyleHandler

    I see that you use VB.NET, while my code sample is C#. But there are conversion tools on the web.

    Best regards

    Wolfgang

  • Posted 17 March 2022, 5:22 am EST

    Hi Said,

    You can use the CellStyle.MergeWith method to merge properties of one cell style into another. Kindly refer to the attached sample showing the same.

    Best Regards,

    Kartik

    PS: Thank you for the suggestions, Wolfgang!

    CellStyleMerge_FG.zip

  • Posted 17 March 2022, 7:02 am EST

    Dear Kartik,

    Thanks for your support.

    It works fine unlikely all rows will be colored in bleu, even if i have a condition in the function.

    I need only to color in bleu if there is document link to the text in column 2, See following code:

    Private Function AssignDGForeColour() As Color
        Try
            For eRow As Integer = 1 To DG.Rows.Count - 1
                If DG(eRow, 4) Is DBNull.Value Or DG(eRow, 4) < 1 Then
                Else
                    'ForeColor
                    Dim csBlue As CellStyle = DGWie.Styles.Add("csBlue")
                    csBlue.ForeColor = Color.Blue
                    DG.Rows(eRow).Style = DG.Styles("csBlue")
    
                    'merge the new csBlue style with the wrapped style using MergeWith method. now the wrapped style also has blue fore color
                    DG.Styles("Wrapped").MergeWith(csBlue)
    
                    'assigning new style to column 2 cells is now not needed
                    'DG.SetCellStyle(eRow, 2, csBlue)
                End If
            Next
    
        Catch ex As Exception
            MsgBox("Error 'AssignDGWieForeColour' :" & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Function
    

    Best regards

    Said

  • Posted 18 March 2022, 3:55 am EST

    Hi Said,

    you don’t apply the style “Wrapped” to any cell.

    Could you post a complete sample? It is hard to say what does wrong with only a code snippet and no screenshots of the current and the expected result.

    When posting code snippets in the forum, please use the “code” tags (button above textbox) - this makes them more readable.

    Best regards

    Wolfgang

  • Posted 18 March 2022, 5:14 am EST - Updated 3 October 2022, 10:36 pm EST

    Dear Wolfgang,

    I really appreciate you support. I will have a look at your link that you send to me in your post. Now i have released the application and cut the merge (Blue/Wrapped).

    You are right. I apply the wrap for each cell but not the bleu color. It is only for those who has linked documents (see attched jpg from the flexgrid). All are in bleu color when using the merge and it is wrong because not all of them has linked documents.

    Best regards

    Said

  • Posted 18 March 2022, 11:02 am EST

    I have to admit I never used the “Row.Style”, I always apply the style to cells only. So I am not sure whether the row style might have side effects.

    Are you sure that your check is correct and some rows really don’t have the blue style?

    No further replies from me for the next two weeks - I am on vacation.

    Best regards

    Wolfgang

  • Posted 18 March 2022, 11:36 am EST

    Have a good vacation.

    Best regards

    Said

  • Posted 22 March 2022, 2:52 am EST

    Hi Said,

    In your code, the wrapped style is assigned to each cell of the second column, and when the csBlue style is merged with the Wrapped style, all the cells using the Wrapped style will also get the blue ForeColor. As per our understanding, you want to use Wrapped style on each cell of the second column and csBlue style on some cells based on your condition. You can easily do so by creating a new style with the Wrapping enabled as well as blue ForeColor and assigning it to the cells of your selected rows based on your condition. Kindly refer to the attached sample showing the same. If you meant something else, could you please share your use case and a small sample showing your issue, so that we can assist you accordingly?

    Best Regards,

    Kartik

    CellStyleMerge_FG_Mod.zip

  • Posted 22 March 2022, 3:25 am EST

    Dear Kartik,

    Is working fine. thanks alot for your support.

    Best regards

    Said

Need extra support?

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

Learn More

Forum Channels