Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

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.

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted

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.

 

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

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...