By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

DataGrid text alignment problems: tall cells, right-truncation

P: n/a
One of the reasons given for the Allied victory in WWI is that the
Nazi armament industry invested so much effort in creating new
weapons (e.g. the jet plane) it wasn't able to develop any of them
to the point of mass-production. There are days when I experience
the same difficulties with C# and dotNET: there are twelve ways to
do 'most _any_thing, and I wind up exploring six before I find an
acceptable solution. <grin>

Case in point: After reading Chris Sells' "Windows Forms
Programming in C#" I'm attempting to warp the DataGrid into a
scrolling three-column information presentation tool to replace an
older approach that was creating (and disposing of) large numbers of
controls (300-odd) on a panel.

With the help of Google and several kind Usenet posters I've managed
to get LinkLabel columns and read-only (uneditable) text columns,
but there are still some points where I could use some advice and/or

1) Right-justified text gets right-truncated (1/2-1 character).
This is a known bug with Labels (known to Usenet readers,
anyway), and the workaround (which I've used elsewhere) is to
tack a "\n" on the end of the text string.

If I were feeding the text directly into the cell there would be
no problem. However, my DataGrid is DataBound to a DataView,
and DataBe a problem. In this case I'm formatting a DateTime
column with "MMMM d"... so there's no place i can see to insert
a "\n". In fact, the VSdotNET documentation on DateTime
Formatting explicitly says this won't work... and when I tried
it anyway <grin> with "MMMM d \n" it only seemed to work

Anyone have any ideas on how I could get right ("Far") aligned
but untruncated text/dates in a DataGrid column?

2) My customer likes whitespace. Since at this point all of my
DataGridColumns are my code, extending the GetHeight() method to
make the cell taller is trivial (and it was nice to see the
other columns stretching to match). This gives me text and
link labels at the top of each cell, but I'd really like to have
them centered. Unfortunately, the DataGridColumn Paint methods
I'm overriding only seem to care about left/right alignment.

Is anyone aware of a way I could get my text up/down centered?

I have a third problem -- when I refresh the DataView this DataGrid
is attached to the DataGrid fails to find the new data -- but I
suddenly realized that I probably need to re-assign the refreshed
(but same-named) DataView to the DataGrid's DataSource. That is,
even if I only have one variable 'dv' I probably need to follow

DataView dv = RefreshDataViewForDG();

with a

dataGrid1.DataSource = dv;

Oh... Now that I've created my new DataGridColumn classes, it would
be really convenient if I could have them as selections in the
DataGrid DataColumnStyles dislog box, which seems to be "stuck" on
DataGridTextBoxColumn and (?)DataGridBoolColumn. Does anyone know
if this is possible?

Ah, well. It's late, and tomorrow will be a much better time to
inspect my feet for fresh bullet holes. <grin>

Any feedback will be appreciated.

Frank McKenney, McKenney Associates
Richmond, Virginia / (804) 320-4887
Munged E-mail: frank uscore mckenney ayut minds pring dawt cahm (y'all)
There is no avoiding war; it can only be postponed to the advantage
of others. -- Niccolo Machiavelli
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.