Results 1 to 3 of 3

Thread: Datagrid / DateTime value problem

  1. #1
    Tycho_Brahe
    Guest

    Question Datagrid / DateTime value problem

    I have a datagrid with lots of data. Some columns are datetime fields. I had to add a DataGrid Table Style in order to see the time as well as the date. That works fine.

    Now, I see the date and time combo in the field but when I do something like MsgBox(DataGrid1.Item(1, 2)) to grab the value it strips out the time portion of the value. That's just test code, but you get the idea.

    In case you're wondering what I'm trying to do: I have a loop that goes through each column and each row and determines how wide the column should be in the Grid by using MeasureString. This would work just fine except when I try to get the string out of the date/time fields it's returning a shorter value (without a date) than it shows in the grid. So, the MeasureString method comes up with an autosize value for the field width that's way too small.

    Any ideas ?

    Thanks,

    Eric

  2. #2
    Tycho_Brahe
    Guest

    Correction

    Correction: When I said "(without a date)" I meant to say "without the time"

  3. #3
    Tycho_Brahe
    Guest

    Found answer

    Is talking to yourself a sign of insanity ? Oh well ;-)

    I found my answer and posted the code in the codebank. I'll include it below. Basically, I should have been using the ToString method for the datacell. I have no idea why (it returned a string before - just without the time), but at least it's a solution.

    ' **********************************************************
    ' Sizes all columns of a datagrid optimally.
    ' Parameters:
    ' TempDG - The datagrid that owns the column
    ' Notes: Ensure that a style exists for the table you are
    ' viewing and that the style name is the same as the table name.
    ' **********************************************************
    Public Sub AutoSizeColumns(ByRef TempDG As DataGrid)
    Dim iCount As Integer
    For iCount = 0 To (TempDG.VisibleColumnCount() - 1)
    AutoSizeCol(iCount, TempDG)
    Next
    End Sub


    ' **********************************************************
    ' Sizes a column of a single datagrid column optimally.
    ' Parameters:
    ' iCol - The column to resize
    ' TempDG - The datagrid that owns the column
    ' Notes: Make sure the column is a valid one. Also, ensure
    ' that a style exists for the table you are viewing and that
    ' the style name is the same as the table name.
    ' **********************************************************
    Public Sub AutoSizeCol(ByVal iCol As Integer, ByRef TempDG As DataGrid)
    Dim iMaxWidth, iTemp As Integer
    Dim iNumRows As Integer = TempDG.BindingContext(TempDG.DataSource, _
    TempDG.DataMember).Count
    Dim G As Graphics
    Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic)
    Dim sTemp, sTableName As String
    Dim Size As SizeF

    ' Get a graphics handle
    G = Me.CreateGraphics

    ' Loop thru all rows of this column. Get length of longest string
    If iNumRows > 0 Then
    For iTemp = 0 To (iNumRows - 1)
    sTemp = TempDG(iTemp, iCol).ToString
    Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
    Size.Width += 10
    If Size.Width > iMaxWidth Then iMaxWidth = Size.Width
    Next iTemp
    End If


    ' Will need tablename to access style
    sTableName = DataGrid1.DataSource.TableName()

    ' Lastly, consider length of column header name
    sTemp = DataGrid1.TableStyles(sTableName).GridColumnStyles(iCol).HeaderText()
    Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
    Size.Width += 10
    If Size.Width > iMaxWidth Then iMaxWidth = Size.Width

    ' Modify the width of the grid column style
    DataGrid1.TableStyles(sTableName).GridColumnStyles(iCol).Width = iMaxWidth
    G.Dispose()
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width