Re: DataGridView, Formatting a Numeric Column
- From: "eBob.com" <eBob.com@xxxxxxxxxxxxxxxx>
- Date: Thu, 3 Jul 2008 22:12:59 -0400
I tried "n0" after you suggested it in your first reply. (I don't ask for
advice and then ignore it!) It just ain't working for me. Here's some of
the code, followed by some comments ...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
DataGridView1.ColumnCount = 5
With DataGridView1.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(DataGridView1.Font, FontStyle.Bold)
End With
With DataGridView1
.Name = "songsDataGridView"
'.Location = New Point(8, 8)
.Size = New Size(500, 250)
'.AutoSizeRowsMode = _
' DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.CellBorderStyle = DataGridViewCellBorderStyle.Single
.GridColor = Color.Black
.RowHeadersVisible = False
.Anchor = CType(AnchorStyles.Bottom Or AnchorStyles.Right _
Or AnchorStyles.Top Or AnchorStyles.Left, AnchorStyles)
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToOrderColumns = True
.ReadOnly = True
.Columns(0).Name = "Directory"
.Columns(1).Name = "FileName"
.Columns(2).Name = "Size"
'.Columns(2).ValueType = GetType(Long)
.Columns(2).DefaultCellStyle.Format = "N0"
.Columns(2).DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleRight
.Columns(2).DefaultCellStyle.Format = "N0"
.Columns(3).Name = ""
.Columns(4).Name = ""
.Columns(4).DefaultCellStyle.Font = _
New Font(Me.DataGridView1.DefaultCellStyle.Font,
FontStyle.Italic)
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
End With
End Sub
If the above looks a bit strange, it is. Most of the code comes from an
example I found. And it's very much a work in progress. I've tried both
"n0" and "N0". And, grasping at straws, I thought maybe I should try
setting the Format after setting the Alignment. I also thought at one point
that maybe I needed to say explicitly what kind of data I was putting in the
column. That's what the commented-out ValueType statement is about. But
note that somehow the framework figures out that the data I am putting in
the column is numeric and provides basic numeric formatting without my
having to say anything about Format. I.E. the ValueType statement makes no
difference.
Well ... I'll be doing some more trashing about in the documentation and the
results of Google searches, but I am not optimistic. So if you have any
further advice I'd be most grateful.
Thanks, Bob
"AMercer" <AMercer@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:687C2EC2-2B6C-42E3-9475-5C7C999264D9@xxxxxxxxxxxxxxxx
Use "n0" instead of "n".
..Columns(col).DefaultCellStyle.Format = "n0"
"eBob.com" wrote:
"AMercer" <AMercer@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A45AB99A-3743-4360-B385-C1858F2CF38C@xxxxxxxxxxxxxxxx
Thanks very much for your help. I now have the number right-aligned.I've spent hours winding my way through the maze of DataGridView
documentation and FINALLY found that
..Columns(2).DefaultCellStyle.Format = "n"
should give me a number with a thousand separator. The numbers in the
column are integers so I don't need any decimal point. But the code
shown
above does no give me a thousand separator and does not right justify
the
values. (Not that the doc said "n" would provide right justification,
but
that's what I'd like and I haven't found even a hint of how to get
it.)
Try "n0" instead of "n". To right align, you need something like
dgv.Columns(Col).DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleRight
re " (Not that the doc said "n" would provide right justification, but
that's what I'd like and I haven't found even a hint of how to get
it.)" -
I
think that if you used "n8", you would format a right justified 8
character
field with comma separators, and that field would then be aligned in
the
dgv
according to DefaultCellStyle.Alignment. This is just an educated
guess -
I
haven't verified it. See also Integer.ToString("n0") - I think
ToString
with
a format string is how dgv renders cell values. "n0" should suffice
for
your
needs.
re - "I've spent hours winding my way through the maze of DataGridView
Documentation". It took me a few days to tame dgv to my satisfaction.
It
is
very flexible and has many optional features, and this gives rise to
its
complexity. The documentation could be better, but with patience, you
will
get it.
BUT I
am still unable to find the right incantation for the thousand separator.
Curiously, few people seem to have run into this based on the few hits my
searches get. (It doesn't help that my Google "Advanced Groups Search"
searches return an overwhelming number of hits from many groups which I
do
not specify!) Which makes me suspect that I am doing something really
stupid!
Are the formatting strings for Integer.ToString() and
..Columns(col).DefaultCellStyle.Format = supposed to be the same? It
would
seem to make sense but I haven't come across any such statement. If I
try
12345.ToString("n") I get "12,345.00". When I specify
..Columns(col).DefaultCellStyle.Format = "n" I do not get the thousand
separator and I do not get the ".00" I don't want the ".00" but I
really
would like the thousand separator.
Thanks again for your help. If you or anyone else has any additional
advice
I'd be very grateful. Bob
.
- Follow-Ups:
- References:
- DataGridView, Formatting a Numeric Column
- From: eBob.com
- Re: DataGridView, Formatting a Numeric Column
- From: eBob.com
- Re: DataGridView, Formatting a Numeric Column
- From: AMercer
- DataGridView, Formatting a Numeric Column
- Prev by Date: Charting data
- Next by Date: Re: DataGridView, Formatting a Numeric Column - Problem Solved
- Previous by thread: Re: DataGridView, Formatting a Numeric Column
- Next by thread: Re: DataGridView, Formatting a Numeric Column - Problem Solved
- Index(es):
Relevant Pages
|