.NET Framework - Setting RightToLeft property for columns in a DataGridView

Asked By DavidLBlackDec on 13-Jan-09 04:45 PM
Is there a way to set the RightToLeft property independently for each column
in a datagridview?

What I want to do is have a datagridview with two columns.  The left hand
column (column 0) is used to type a Hebrew word.  The right hand column
(column 1) is used to type an English word.

I have tried usind the CellEnter event.  On that event, I figure out which
column has been entered, and I set the currentinputlanguage to be English or
Hebrew, as appropriate.  That works.

Also, if the user entered the Hebrew column, I tried setting the RightToLeft
property of the datagridview to RightToLeft.yes, and if he entered the
English column set the property to RightToLeft.No.  That part doesn't work so
well.  The RightToLeft part works, but it changes the order of the columns,
making it very difficult to enter data.

What I want is for the columns to remain in the same order, but type
righttoleft in one column, and lefttoright in the other column.



(Note:  This is an extremely similar problem to two others made recently,
but I have made some changes to the code since then, and this version of the
question is a bit more succinct.  I'm hoping that leads to someone
understanding, and answering, the question.)

, and changing the RightToLeft property for the control.  That almost works,
except that the position of the columns changes




v-lli replied on 13-Jan-09 09:56 PM
Hi David,

column in a datagridview?

We can only set the RightToLeft property on a Control. Since a
DataGridViewColumn is not a control, we cannot set the RigthToLeft property
on it.

In fact, when a DataGridViewCell is in edit mode, an editing control is
hosted inside this cell for the user to edit value. We can set the
RightToLeft property of this editing control and this is really what you
want. To do this, handle the EditingControlShowing event of the
DataGridView and get the hosted editing control and then set its
RightToLeft property. The following is a sample.

void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (e.Control != null)
{
if (dgv.CurrentCell.ColumnIndex == 1)
{
e.Control.RightToLeft = RightToLeft.Yes;
}
else
{
e.Control.RightToLeft = RightToLeft.No;
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
DavidLBlackDec replied on 15-Jan-09 09:48 AM
Thanks.  That almost works.  It's a big improvement.  However, I still have a
problem.  It seems to work if there is no punctuation.

I type in


.אני אוכל

(That means, "I eat.")

It looks great, while editing.  Then, I leave the cell, or just finish the
edit,and it turns into:

אני אוכל.

(Note that the period is now on the right hand side, although I entered it
on the left hand side.)

This plays havoc with sentences, or anything with parentheses.


(
v-lli replied on 15-Jan-09 11:44 PM
Hi David,

Thank you for your reply!

When a DataGridViewTextBoxCell leaves edit mode, the hosted editing control
is detached from the cell and the cell draws the cell's value. By default,
a DataGridViewTextBoxCell draws text from left to right.

To show text from right to left in a DataGridViewTextBoxCell, handle the
CellPainting event of the DataGridView and draw text in the cells you want
from right to left.

The following is a sample.

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex >=0)
{
e.PaintBackground(e.CellBounds, true);
TextRenderer.DrawText(e.Graphics,
e.FormattedValue.ToString(), e.CellStyle.Font, e.CellBounds,
e.CellStyle.ForeColor, TextFormatFlags.RightToLeft);
e.Handled = true;
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
DavidLBlackDec replied on 02-Feb-09 11:04 AM
I got a chance to try this, but my value of e throws an exception when
accessing its properties.  I suppose I will have to set some value so that
DavidLBlackDec replied on 02-Feb-09 11:41 AM
After a little bit of fiddling in my code, it seems to work.  Thanks.

Thought for the day.  The datagridview is a very common control that is
asked to do all sorts of things.  If you are using it to render the contents
of a database in a straightforward fashion, it works easy.  Of course, there
are ways to put checkboxes and pictures and all sorts of fields in there.

The problem I have is that it's very difficult to work through how to do all
these things.  There's a lot of searching through examples on the MSDN libray
and googling involved.

If there were a book written about "Using the .Net Framework Datagridview"
or "Displaying tables in the .NET Framework", and that book went into all the
details, like custom painting of cells, I would buy it.

If anyone knows of such a book, or something that would provide the
equivalent information, please post a reply.
v-lli replied on 02-Feb-09 11:42 PM
Hi David,

Thank you for your reply! I'm glad to hear that the problem is solved now.

As for book about DataGridView programming, I do a search but don't find a
good candidate that introduces every aspect of DataGridView. Any way, the
MSDN document is a vast knowledge base which you can refer to.

Visit the following link for DataGridView programming:
http://msdn.microsoft.com/en-us/library/e0ywh3cz.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Chris Watt replied to v-lli on 02-Dec-10 01:34 PM
Linda,



I found this thread and wondered if you could help.  I have a vb.net form that is displaying data in a datagridview control.  My issue is that one of the data columns contains strings of multiple languages and I want to display them correctly (left-to-right or right-to-left).  I'm able to determine which strings need further formatting by checking the value of the Unicode characters.  I've tried using the example you've provided above and am able to change the forecolor of the selected strings but cannot seem to get them to display right-to-left when necessary. A simple test version of the CellPainting event code is here:



Private Sub DGV_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting

If (e.ColumnIndex = 30 And e.RowIndex >= 0) Then

e.PaintBackground(e.CellBounds, True)

Dim strVal As String = e.FormattedValue.ToString

Dim i As Integer = AscW(Microsoft.VisualBasic.Left(strVal, 1))

If i > 385 Then

TextRenderer.DrawText(e.Graphics, e.Value.ToString, e.CellStyle.Font, e.CellBounds, System.Drawing.Color.Red, TextFormatFlags.RightToLeft)

Else

TextRenderer.DrawText(e.Graphics, e.Value.ToString, e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor)

End If

e.Handled = True

End If

End Sub





What am I missing?
Chris Watt replied to v-lli on 02-Dec-10 01:36 PM
Linda,



I found this thread and wondered if you could help.  I have a vb.net form that is displaying data in a datagridview control.  My issue is that one of the data columns contains strings of multiple languages and I want to display them correctly (left-to-right or right-to-left).  I'm able to determine which strings need further formatting by checking the value of the Unicode characters.  I've tried using the example you've provided above and am able to change the forecolor of the selected strings but cannot seem to get them to display right-to-left when necessary. A simple test version of the CellPainting event code is here:



Private Sub DGV_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting

If (e.ColumnIndex = 30 And e.RowIndex >= 0) Then

e.PaintBackground(e.CellBounds, True)

Dim strVal As String = e.FormattedValue.ToString

Dim i As Integer = AscW(Microsoft.VisualBasic.Left(strVal, 1))

If i > 385 Then

TextRenderer.DrawText(e.Graphics, e.Value.ToString, e.CellStyle.Font, e.CellBounds, System.Drawing.Color.Red, TextFormatFlags.RightToLeft)

Else

TextRenderer.DrawText(e.Graphics, e.Value.ToString, e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor)

End If

e.Handled = True

End If

End Sub





What am I missing?
Chris Watt replied to v-lli on 02-Dec-10 01:37 PM
Linda,



I found this thread and wondered if you could help.  I have a vb.net form that is displaying data in a datagridview control.  My issue is that one of the data columns contains strings of multiple languages and I want to display them correctly (left-to-right or right-to-left).  I'm able to determine which strings need further formatting by checking the value of the Unicode characters.  I've tried using the example you've provided above and am able to change the forecolor of the selected strings but cannot seem to get them to display right-to-left when necessary. A simple test version of the CellPainting event code is here:



Private Sub DGV_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting

If (e.ColumnIndex = 30 And e.RowIndex >= 0) Then

e.PaintBackground(e.CellBounds, True)

Dim strVal As String = e.FormattedValue.ToString

Dim i As Integer = AscW(Microsoft.VisualBasic.Left(strVal, 1))

If i > 385 Then

TextRenderer.DrawText(e.Graphics, e.Value.ToString, e.CellStyle.Font, e.CellBounds, System.Drawing.Color.Red, TextFormatFlags.RightToLeft)

Else

TextRenderer.DrawText(e.Graphics, e.Value.ToString, e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor)

End If

e.Handled = True

End If

End Sub





What am I missing?