0

How can I set text direction of an specific column in datagridview without changing whole datagridviews direction? (I mean text direction change for right to left languages and it's not same as text-alignment)

Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
  • I would probably just reverse the text before binding. What kind of datasource are you using? – Jason Geiger Aug 17 '15 at 14:07
  • @JasonGeiger I'm using MySql datasource. Reversing text to direction support is a hard job and needs some proccess. I hope to be a simpler way like setting a property or do some simple tricks without much of coding – Mojtaba Rezaeian Aug 17 '15 at 14:11
  • @JasonGeiger and also reversing text may cause more trouble if user want to copy text to clipboard and use it somewhere because it will have fake value just for correct preview in output. – Mojtaba Rezaeian Aug 17 '15 at 14:13
  • I understand what you are saying but I believe that "text-direction" is a system language default. Mixing the languages by control is going to be confusing for sure. I would fake it by selecting the reverse text from MySQL. Example, SELECT Field1, REVERSE(Field2) AS Field2... – Jason Geiger Aug 17 '15 at 14:18
  • @JasonGeiger Unfortunately mysql REVERSE() function is failing to show correct output because for a value like `0936 405 8889` which is a contact number it is shown in right to left direction (datagridview is set to right to left) like `8889 405 0936` and if I reverse its text using mysql, it completely fails and is showing `6390 504 9888` but the correct value output should be `0936 405 8889` – Mojtaba Rezaeian Aug 17 '15 at 14:28

3 Answers3

1

I know it's not the best answer but temporary I have handled CellPainting event to change text direction of that specific column:
(in my example datagridview is RightToLeft and I marked column 5 to be painted in LeftToRight direction)

Public Sub DGV_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
    If e.ColumnIndex = 5 And Not Object.Equals(e.Value, DBNull.Value) Then
        e.PaintBackground(e.CellBounds, False)
        TextRenderer.DrawText(e.Graphics, e.Value, e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, e.CellStyle.BackColor, (TextFormatFlags.Left Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter) And (Not TextFormatFlags.RightToLeft))
        e.Handled = True
    End If
End Sub

but still I'm looking for a more comfortable trick/solution to do this because this solution still lacks when editing the text and also handling paint event is not showing very beautiful results as I expect.

Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
  • This solution is all I could find too... have a look at [this MSDN atricle](http://blogs.msdn.com/b/vsarabic/archive/2009/01/29/datagridview-with-rtl-columns.aspx), it seems to show a way to get editing to work, but it's still overriding CellPainting... – DrewJordan Aug 18 '15 at 12:14
0

The functionality you are telling is not directly available, however you can achieve it via formatting string for that particular column [ex. string.Format(...)] whatever the format you want, before you bind data to the DataGridView.

Kunal Khatri
  • 451
  • 3
  • 12
  • I doubt the String.Format() function could fix the text direction. Would you give me an example how to do this? – Mojtaba Rezaeian Aug 17 '15 at 15:44
  • Actually i took the question differently, anyway, For, text direction change for right to left languages, and vice versa you can create object of CultureInfo with required culture, and can decide at runtime if(cultureinfo.TextInfo.IsRightToLeft== true), and change the alignment of cell accordingly - like - this.dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; – Kunal Khatri Aug 18 '15 at 07:41
  • Thank you for your time & answer but yes your answer is about text alignment and it is out of my question about text-direction. Thanks anyway :) – Mojtaba Rezaeian Aug 18 '15 at 10:16
0

I don't have a complete example for this, but it should work to create a CellTemplate for the column and use whatever CultureInfo is appropriate:

grdCheck.Columns[0].CellTemplate.Style = new DataGridViewCellStyle{FormatProvider = CultureInfo.CurrentCulture};
DrewJordan
  • 5,266
  • 1
  • 25
  • 39