C1FlexGrid one column 'right-to-left'

Posted by: arno_hendrikx on 14 July 2019, 11:37 pm EST

    • Post Options:
    • Link

    Posted 14 July 2019, 11:37 pm EST

    Hi,

    we’ve been looking around but could not find any solution.

    In a grid we display numerous translations in different columns.

    One column is Arabic. We would like to have this column displayed ‘right-to-left’ .

    Does anyone know the trick do do this?

    Thanks!

  • Posted 15 July 2019, 6:39 pm EST

    Hi,

    actually, there is a property “CellStyle.StringFormat”, which is of type “System.Drawing.StringFormat”. This one has a property “FormatFlags” which can be set to “StringFormatFlags.DirectionRightToLeft”.

    But setting this property has no effect - probably because the StringFormat property is created internally based on other information.

    So it is up to the C1 guys to research this…

    Best regards

    Wolfgang

  • Posted 17 July 2019, 6:26 pm EST

    Hi,

    As mentioned by Wolfgang, you can use the StringFormat class to do this.

    You would however need to draw the text in OwnerDrawCell event:

    private void C1FlexGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
    {
        if (e.Col == 2 && e.Row >= c1FlexGrid1.Rows.Fixed)
        {
            string text = e.Text;
            Font font = e.Style.Font;
            Color foreColor = e.Style.ForeColor;
            RectangleF rect = e.Bounds;
            StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
    
            e.Graphics.DrawString(text, font, new SolidBrush(foreColor), rect, format);
            e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.All & ~C1.Win.C1FlexGrid.DrawCellFlags.Content);
        }
    }
    

    Please refer the attached sample for complete example.

    RTL_Columns.zip

    Regards,

    Jitender

  • Posted 17 July 2019, 8:57 pm EST

    Thanks Jitender!

    Works like a charm!

  • Posted 18 July 2019, 10:08 pm EST - Updated 3 October 2022, 3:59 pm EST

    Hi Jitender,

    After implementing your solution, the correct displaying of RTL-languages in a grid is achieved, but I’m faced with 2 other issues:

    1. When a row is selected (SelectionMode = row, HighLight = Always), the OwnerDrawn cells display a solid selection color with the contents hidden, wheras the ‘normal’ cells display a blurred selection color with the content visible. Please see the attached images.



    1. When editing a OwnerDrawn cell, changes are not committed to the cell, wheras the ‘normal’ cells behave as expected. For the OwnerDrawn cells I use ```

      Editor = New TextBox() With {.RightToLeft = RightToLeft.Yes, .BorderStyle = BorderStyle.None}
    
    Any thoughts on these issues?
  • Posted 18 July 2019, 10:09 pm EST - Updated 3 October 2022, 3:59 pm EST

    Hi Jitender,

    These images are better:

  • Posted 21 July 2019, 3:29 pm EST

    Hi,

    1. I’m sorry I did not consider Row selection. If you’re using Row selection then you would need to first ask the grid to draw the background and then use DrawString to draw the content. You should change your code to this:
    private void C1FlexGrid1_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
    {
        if (e.Col == 2 && e.Row >= c1FlexGrid1.Rows.Fixed)
        {
            string text = e.Text;
            Font font = e.Style.Font;
            Color foreColor = e.Style.ForeColor;
            RectangleF rect = e.Bounds;
            StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
    
            e.DrawCell(DrawCellFlags.All & ~DrawCellFlags.Content);
            e.Graphics.DrawString(text, font, new SolidBrush(foreColor), rect, format);
        }
    }
    
    1. I could not observe this. Can you please share a sample where I can see the issue?

    Thanks,

    Jitender

  • Posted 21 July 2019, 10:12 pm EST - Updated 3 October 2022, 3:59 pm EST

    Hi Jitender,

    So simple, I should’ve thought of that myself, thank you!! It solves both issue 1 (more or less, see below) and issue 2 (completely), so no need to investigate issue 2 any further.

    For issue 1, please observe the difference in the backcolor and forecolor of a selected row between ‘normal’ cells (left column, “Grieks”, middle column, “Russisch”) and owner drawn cells (right column, “Arabisch”), when the cursor is not on the owner drawn cell:

    The grid is set to SelectionMode: Row, HighLight: Always. The owner drawn cells appear to be presented in Style: Highlight, on contrast to the ‘normal’ cells

    How can I control the selected backcolor and forecolor of owner drawn cells, so that they are the same as with ‘normal’ cells?

    All is OK when the cursor is on the owner drawn cell:

    Thank you very much for your quick replies!!

  • Posted 22 July 2019, 3:02 pm EST - Updated 3 October 2022, 3:59 pm EST

    Hi,

    It should work correctly since we’re using DrawCell to draw the background which would use the correct Style.

    Here’s what I’m observing:

    Which VisualStyle are you using? If you can attach a sample where this can be observed I can see what’s causing this issue.

    Thanks,

    Jitender

  • Posted 22 July 2019, 8:39 pm EST

    Hi Jitender,

    It’s quite an extensive project, so it’s hard to create a matching sample. I’m using VisualStyle = Office2010Silver by the way, with C1.Win.C1FlexGrid.4 version 4.0.20163.212. Updating the project to a later or the latest version is not an option at this stage of development.

    Thanks for your insights!

  • Posted 24 July 2019, 3:08 am EST

    Hi Jitender,

    It looks to me like for ‘normal’ cells, selection forecolor/backcolor is derived from VisualStyle, while for OwnerDrawn cells, selection forecolor/backcolor is derived from Styles.Highlight. How can I ‘overrule’ that behaviour? In other words: How can I re-apply VisualStyle to OwnerDrawn cells?

    As a workaround, I used a color picker to get the selection backcolor/forecolor (in HSL/RGB) of the applied VisualStyle and assigned these colors to backcolor/forecolor of Styles.Highlight.

    Not an ideal solution, because when changing the VisualStyle, I have to change Styles.Highlight as well. On all affected grids, and believe me, there are a lot in this project!

  • Posted 24 July 2019, 4:56 pm EST

    Hi,

    You’re right that OwnerDrawCell’s background uses the grid’s style (‘Highlight’ style when the cell is part of the selection). But the Highlight style itself should change with VisualStyle.

    I also used Office2016 VisualStyle with 2016v3 build of FlexGrid but it still works correctly.

    Can you please try the attached sample and see if you observe the issue in the sample? If you don’t observe the issue with the sample, then please modify it so that we can replicate the issue at our end.

    FlexGrid.RTL_Columns.zip

    Thanks,

    Jitender

  • Posted 24 July 2019, 10:24 pm EST

    Hi Jitender,

    Your sample works like a charm. I’m using VB by the way, but can use/interpret C# just fine.

    My observation: in my case, Styles.Highlight did not adjust to the Office2010Silver VisualStyle, where in your case it did (I presume you didn’t change it manually…).

    The difference between mine and yours: you set DrawMode = Normal at design-time and DrawMode = OwnerDraw at runtime, where I set DrawMode = OwnerDraw at design-time. I’ll try to see if that makes any difference. Maybe a reset on all the styles will do the trick.

    So, my workaround as mentioned is the solution for now. Maybe I’m doing something wrong, but I’ll try to figure that out in a later stage.

    Thanks for your help!

    A little off-topic, but related: is there a way to set a C1Editor in left-to-right modus? Or should I just use HTML style or markup?

  • Posted 25 July 2019, 3:43 pm EST

    Hi,

    The only reason that I can think for this issue could be that you had modified the Highlight styles and then changed VisualStyle. Note that if the style info is changed before switching VisualStyle, the grid assumes that you want to keep those changes so the properties from VisualStyle are not applied to the changed Style properties.

    If that’s the case then resetting all the styles should work.

    For enabling right-to-left in C1Editor, you can use a simple CSS like this:

    body {
        direction: rtl;
    }
    

    And then load the CSS file:

    C1Editor1.LoadDesignCSS("path\to\main.css")
    

    Regards,

    Jitender

  • Posted 26 July 2019, 2:25 am EST

    Hi Jitender,

    C1Editor in RTL-mode: Works like a charm, although my code has no need for a CSS-file on disc:

    Dim buffer As Byte() = Encoding.UTF8.GetBytes("body {direction: rtl; unicode-bidi: bidi-override;}")
    _C1Editor.LoadPreviewCSS(New MemoryStream(buffer))
    

    One tiny RTL-related question left (if I may): I have a column with a DataMap. The column itself is showing RTL, using your excellent method we’ve been talking about before. But, the ComboBox representing the DataMap when editing a cell in this column, is still LTR. How can I address this ComboBox to set it to RTL?

  • Posted 28 July 2019, 7:31 pm EST

    Hi,

    The DataMap combo box is an extension of the default ComboBox which has RightToLeft property. So, handling it in the SetupEditor event should work:

    private void C1FlexGrid1_SetupEditor(object sender, RowColEventArgs e)
    {
        if (e.Col == 3)
        {
            ComboBox comboBox = c1FlexGrid1.Editor as ComboBox;
            if (comboBox != null)
            {
                comboBox.RightToLeft = RightToLeft.Yes;
            }
        }
    }
    

    However, this is not working as expected so I’ve asked the developers to look into this. I will let you know as soon as we get any information from them [Internal Tracking ID: 392018].

    Regards,

    Jitender

  • Posted 29 July 2019, 12:14 am EST - Updated 3 October 2022, 3:59 pm EST

    Hi Jitender,

    It looks like the ComboBox itself is RightToLeft orientated, but the contents, both in the TextBox and the DropDownList is stll LeftToRight.

    Private Sub ArticleIngredients_SetupEditor(sender As Object, e As RowColEventArgs) Handles ArticleIngredients.SetupEditor
    
        With ArticleIngredients
            If Not (e.Row < .Rows.Fixed) AndAlso e.Col = .Cols("fId").Index Then
                If Not IsNothing(.Editor) Then
                    Dim cb As ComboBox = DirectCast(.Editor, ComboBox)
                    cb.RightToLeft = If(SqlIsRTLLanguage(Me.LanguageID), RightToLeft.Yes, RightToLeft.No)
                End If
            End If
        End With
    
    End Sub
    

    Nice tip adressing the ComboBox, by the way, thank you. I was probing _C1FlexGrid.Cols(e.Col).Editor… which - to me - seemed the place to be.

  • Posted 29 July 2019, 1:09 am EST - Updated 3 October 2022, 3:59 pm EST

    Hi Jitender,

    I found (somewhat of) a solution:

    Private Sub ArticleIngredients_SetupEditor(sender As Object, e As RowColEventArgs) Handles ArticleIngredients.SetupEditor
    
        With ArticleIngredients
            If Not (e.Row < .Rows.Fixed) AndAlso e.Col = .Cols("fId").Index Then
                If Not IsNothing(.Editor) Then
                    Dim cb As ComboBox = DirectCast(.Editor, ComboBox)
                    cb.DrawMode = DrawMode.Normal
                    cb.RightToLeft = If(SqlIsRTLLanguage(Me.LanguageID), RightToLeft.Yes, RightToLeft.No)
                End If
            End If
        End With
    
    End Sub
    

  • Posted 29 July 2019, 3:16 pm EST

    Hi,

    Thanks for sharing your workaround.

    It seems that the grid’s ComboBox (by default) uses custom drawing for its items. Since the grid does not know that we’re using RightToLeft in a specific column, it is still using the default LeftToRight layout for the combo box items.

    Regards,

    Jitender

  • Posted 29 July 2019, 6:18 pm EST

    Hi Jitender,

    Case closed for now, thanks for your help.

    I would love for the C1FlexGrid to be able to set the RightToLeft property on individual columns (and maybe rows or even cells), without having to use DrawMode = OwnerDraw and the OwnerDrawCell event. That way, a columns Editor or DataMap could inherit the setting.

    So please, if you can, make that a formal feature request.

  • Posted 29 July 2019, 11:05 pm EST

    Hi Jitender,

    I found the definitive workaround for now for me:

    Private Sub ArticleIngredients_SetupEditor(sender As Object, e As RowColEventArgs) Handles ArticleIngredients.SetupEditor
    
        With ArticleIngredients
            If Not (e.Row < .Rows.Fixed) AndAlso e.Col = .Cols("fId").Index Then
                If Not IsNothing(.Editor) Then
    
                    Dim cb As ComboBox = DirectCast(.Editor, ComboBox)
    
                    cb.RightToLeft = If(SqlIsRTLLanguage(Me.LanguageID), RightToLeft.Yes, RightToLeft.No)
                    AddHandler cb.DrawItem, AddressOf ComboBox_DrawItem
                End If
            End If
        End With
    
    End Sub
    

    Private Sub ComboBox_DrawItem(sender As Object, e As DrawItemEventArgs)

    Dim cb As ComboBox = DirectCast(sender, ComboBox)
    
    If (cb.RightToLeft = RightToLeft.Yes) Then
    
        Dim C1FG As C1FlexGrid = DirectCast(cb.Parent, C1FlexGrid)
    
        e.DrawBackground()
        e.Graphics.FillRectangle(New SolidBrush(If((e.State And DrawItemState.Selected) = DrawItemState.Selected, C1FG.Styles.Highlight.BackColor, e.BackColor)), e.Bounds)
        e.Graphics.DrawString(cb.Items(e.Index).ToString, e.Font, New SolidBrush(If((e.State And DrawItemState.Selected) = DrawItemState.Selected, C1FG.Styles.Highlight.ForeColor, e.ForeColor)), e.Bounds, New StringFormat(StringFormatFlags.DirectionRightToLeft))
        If ((e.State And DrawItemState.Selected) = DrawItemState.Selected) Then ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds)
    End If
    

    End Sub

    The ComboBox associated with the grids Editor when a column has a DataMap assigned, has DrawMode = OwnerDrawVariable, so a DrawItem event will fire when added...
  • Posted 30 July 2019, 3:45 pm EST

    Hi,

    I’ve asked the developers to consider using the properties set in CellStyle.StringFormat for your use case. If the developers find it feasible, this will be added in a future release [Internal Tracking ID: 392402].

    Regards,

    Jitender

Need extra support?

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

Learn More

Forum Channels