Dealing with WinForms Combobox and name/value items.#

I just read this article on aspalliance.com by Sandeep Archarya.

I think the article had 2 main points.  One was that databinding a combobox is not "bug free" because the act of manually databinding to a combobox causes the SelectedIndexChanged event to fire.  The second point was to show how to create a simple namevalue class to be added to the combobox items collection to get around the "problem" of combobox's taking an object as the item in it's list (as opposed to a name value pair).

I would point out the following:

First, I am not sure that I would call the SelectedIndexChanged event firing a bug.  When you manually databind a list, the first item become selected by default, and thus the selectedindex HAS changed, from -1 to 0.

Second, if you consider this a problem you can get around it a couple different ways.

1) Don't use manual databinding.

In the designer, select a datasource or create a new one.  I created a simple employee object and created an object datasource for this employee.  This creates a EmployeeBindingSource object that I bind my employee list to.  Using this method, I didn't experience the SelectedIndexChanged event firing on the databind.

Private list As New System.Collections.Generic.List(Of employee)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '*** create some fake employee objects
   For i As Integer = 70 To 80
      list.Add(New employee(i, Chr(i)))
   Next

    '*** this will populate our control (Combobox1)
   Me.EmployeeBindingSource.DataSource = list

End Sub

2) You can use manual databinding, and manually detach and attach the event handler for this event.

Private list As New System.Collections.Generic.List(Of employee)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    '*** create some fake employee objects
   For i As Integer = 70 To 80
      list.Add(New employee(i, Chr(i)))
   Next

    '*** remove the handler
    RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    '*** databind
    Me.ComboBox1.ValueMember = "id"
    Me.ComboBox1.DisplayMember = "name"
    Me.ComboBox1.DataSource = list
    '*** add the handler back
    AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End Sub

 

Categories:  |  |  | 
Friday, November 10, 2006 4:32:43 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Data Driven Checkbox (Horizontal) Lists#

This control is pretty cool.  It makes it pretty easy to databind data into a list of checkboxes.

http://developer.coreweb.com/articles/Default6.aspx

Categories:  |  |  | 
Wednesday, September 06, 2006 4:54:56 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Rocky Lhotka and Csla.Net/Asp.Net on DnrTV#

Rochy Lhotka appeared again on DNR-TV for 2 more segments focusing on CSLA in ASP.Net and WebServices:

http://www.dnrtv.com/default.aspx?showID=32

http://www.dnrtv.com/default.aspx?showID=33

Having successfully completed a CSLA.Net project for Wilson Sporting Goods I am interested to see what is different in the web side vs the winform side.

Categories:  |  |  | 
Wednesday, September 06, 2006 3:39:48 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Databinding "Bind()" in ASP.NET#

If you used the developemt tools to create a datagrid in ASP.NET 2.0, it will show something like:

NavigateUrl='<%# Bind("EmployeeId") %>'

But what if you wanted to do something more complicated that just ge the EmployeeId into the field, like for example if you wanted to run a JS function.  Then you have to do away with Bind and use the more verbose DataBinder method like this: 

NavigateUrl='<%# "javascript:UseThisJobIndex(" & DataBinder.Eval(Container.DataItem,"JobIndex") & ")" %>'

That's all there is to it.

 

Categories:  |  |  | 
Thursday, August 24, 2006 12:16:56 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

All content © 2009, Christopher May, Inc
Open Job Positions
On this page
Google Ads
This site
Calendar
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: