Formatting of dataTable columns

  • Thread starter Thread starter dee
  • Start date Start date
D

dee

Hi,

i have a price like 13.10 or 13.00

When i add this to dataTable , then bind this to the datagrid i get

13.1 and 13

i am loosing the trailing zero on the datagrid , is there a way to get
the zeros back. I think this is possible with a table style but is
there a simpler way before resorting to that . I tried rounding the
number decimal.round , this doesnt have any effect , its changing it
in the datatable.

David
 
Data displayed in DataGrid is formatted through DataGrid's TableStyles. A
DataGrid can have 1 to many TableStyles, mapping to different
DataTable/DataView, so that you can use one DataGrid to display different
data from different source (different DataTable/DataView). If you do not
define a table style, DataGrid uses a default one that basically does no
formatting. TableStyle also allows you to decide which columns you want to
display in DataGrid.

To define TableStyle in DataGrid, simply click TableStyles property in
DataGrid's properties window; To format each column, in "TableStyles
Connection: window, click "GridColumnStyles" property.
 
When i try to use the table style i get the following error

Additional information: The '' DataGridColumnStyle cannot be used
because it is not associated with a Property or Column in the
DataSource.

it occurs in a method i am using to resize the datagrid

Public Sub AutoSizeCol(ByVal col As Integer, ByVal dg As DataGrid,
ByVal hide As Boolean)
Const minWidth = 45
Dim width As Single
width = 0
If hide = True Then ' No need to do anything else

Else
Dim numRows As Integer
numRows = CType(dg.DataSource, DataTable).Rows.Count
Dim g As Graphics
g = Graphics.FromHwnd(dg.Handle)
Dim sf As StringFormat
sf = New StringFormat(StringFormat.GenericTypographic)
Dim size As SizeF
Dim i As Integer
i = 0
Do While (i < numRows)
size = g.MeasureString(dg(i, col).ToString, dg.Font, 500,
sf)

If (size.Width > width) Then
width = (size.Width + 16)
End If
i = (i + 1)

Loop
'Set min width
If width < minWidth Then width = minWidth
g.Dispose()
End If
dg.TableStyles(0).GridColumnStyles(col).Width = CType(width,
Integer)
'DataGrid1.TableStyles(0).GridColumnStyles(col).DataGridTableStyle.BackColor
= System.Drawing.Color.Aqua
End Sub
 
Back
Top