Use Binding to Format DateTime in ListBox?

VagabondSW

Regular
Joined
Feb 19, 2005
Messages
66
I have a ListBox with the DataSource property assigned to an ArrayList. The ListBox displays the distinct values of a Column selected in another ListBox. So, depending on the Column selected in the other ListBox, the ArrayList could contain values that are all Integer, String, DateTime, etc. Although the ArrayList does not implement the IBindingList interface, the MSDN description of the Binding Class confirms that an ArrayList can be used in a Binding.

When the ArrayList contains DateTime values, the ListBox displays them in the default format:

11/20/2002 12:00:00 AM​

I only want to display the Date, not the Time. Assuming (and it is an assumption) that it would be more efficient to Format the DateTime values using a Binding object, how would I actually implement the event handlers on a conditional basis? Might it be easier and more efficient to simply format each value in the list (we could be talking about thousands)?

My UpdateValueList method accepts the selected column As DataColumn argument and uses that to extract the distinct values.
Visual Basic:
Private m_valueList As ArrayList

Private Sub UpdateValueList(ByVal column As DataColumn)

  'Some code was omitted for the sake of brevity...
  Dim bmb As BindingManagerBase
  Dim valueBinding As Binding
  Dim value As Object

  'Only apply Formatting when necessary.
  If column.DataType Is GetType(System.DateTime) Then
    valueBinding = New Binding("Items", m_valueList, String.Empty)
    Me.lstboxValues.DataBindings.Add(valueBinding)
  End If

  'The ArrayList does not implement the IBindingList interface.  I call
  'the SuspendBinding and ResumeBinding methods of the BindingManagerBase
  'instance to force the data bound control to be updated.
  bmb = BindingContext(Me.m_valueList)
  bmb.SuspendBinding()
  For Each row As DataRow In drc
    value = row(columnName)
    If Not TypeOf (value) Is System.DBNull Then
      If Not m_valueList.Contains(value) Then
        m_valueList.Add(value)
        index += 1
      End If
    End If
  Next
  m_valueList.Sort(Comparer.DefaultInvariant)
  bmb.ResumeBinding()
End Sub

Somewhere, somehow, I would still need to capture the Format event for (presumably) the valueBinding object and apply the formatting in that event. Any help, advice, or links are greatly appreciated.
 
Nevermind...

In addition to asking a convoluted question, I pieced together a convoluted solution using the Format event of the Binding class and found it was incredibly inefficient. Because of the way I am using the ListBox.DataSource property, it makes a lot more sense, in terms of performance and implementation, to simply format the DateTime values individually when I have DateTime values in my data bound ArrayList.

Visual Basic:
Private m_valueList As ArrayList

Private Function FormatDateValue(ByVal value As System.Object) As Object
  Dim dt As DateTime

  dt = DirectCast(value, System.DateTime)
  value = dt.ToShortDateString

  Return value
End Function

'This snippet was inserted into the For Each Loop of the 
'UpdateValueList method included in the first post in this thread.
If TypeOf (value) Is System.DateTime Then
  value = Me.FormatDateValue(value)
End If
 
Back
Top