Skip to main content

Articles

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Windows Mobile Developer Controls

Techniques for filling lists using the .NET Compact Framework

Written by Derek Mitchell  [author's bio]  [read 46145 times]
Edited by Derek

Download the code

Page 1  Page 2 

Different techniques for filling lists using the .NET Compact Framework

I started writing this tutorial last week before I got sidetracked by the SQLCEDataAdapter. In essence as I was putting together the test data for this tutorial I realized that the technique being used to create the SQL Server CE database was a tutorial in and of itself. So if you have any questions about the attached code relating to creating the test data read the .NET Compact Frameworks & the ADO.NET Data Adapter article where I explain how I bridged the data from an XML data source into a SQL CE database.

So you've embraced the .NET Compact Framework and you're nashing your teeth trying to find out how to add items to a list or combo-box. Or you've heard that the performance of binding the dataset to the list is terrible! Well hopefully we can kill two birds with one stone. In this tutorial we will investigate two techniques to fill a combo-box and measure the performance of each. The first technique binds a data view to the combo-box and the second is much classier. (er a little weak in the humor department but it is 1:30 am...)

Using a dataview

The easiest way to get table information into a combo-box is to expose a table from a data set using thedata view. Take a look at this code used to fill the combo-box cbo1:

Dim oTimeIt As New TimeIt()
Dim dvTest As DataView
If sql.ceConnect Then
mFilling = True
cbo1.Items.Clear()
data.Clear()
cbo1.Visible = False
sbar.Text = "Filling combobox using dataset." : sbar.Refresh()
oTimeIt.Start()
Dim SqlDATest As New SqlCeDataAdapter("SELECT DevbuzzID, TestName FROM DevbuzzTable order by TestName", sql.connCE)
SqlDATest.Fill(data, "DevbuzzTable")
dvTest = New DataView(data.Tables("DevbuzzTable"))
cbo1.DataSource = dvTest
cbo1.DisplayMember = "TestName"
cbo1.ValueMember = "DevbuzzID"
oTimeIt.Finish()
Label1.Text = oTimeIt.Elapsed() & " seconds"
cbo1.Visible = True
sbar.Text = "Finished." : sbar.Refresh()
SqlDATest = Nothing
sql.Disconnect()
mFilling = False
End If

Firstly we grab the test data from the SQL Server CE database using:

Dim SqlDATest As New SqlCeDataAdapter("SELECT DevbuzzID, TestName FROM DevbuzzTable order by TestName", sql.connCE)
SqlDATest.Fill(data, "DevbuzzTable")

You will notice the terrible syntax (at least in my mind) of the data adapter fill method. For a long time I couldn't work out why I find the data adapter syntax confusing - then it hit me - instead of SqlDATest.Fill(data, "DevbuzzTable") it should really be data.Fill(SqlDATest, "DevbuzzTable") - think about it, the data data set it being filled not the SQLDATest data adapter. Well now that I have that little rant off my chest we can move onto the exposing the DevbuzzTable test data using the data view. This is the command:

dvTest = New DataView(data.Tables("DevbuzzTable"))

Now all we need to do is bind the combo-box to the dvTest dataview. We bind the column we want to display to the DisplayMember property and the key we wish to return to the ValueMember property and we're done! When you a click on the combo-box you can access the value of the corresponding ValueMember using the following code:

Dim row As DataRowView = CType(cbo1.Items(cbo1.SelectedIndex), DataRowView)
MessageBox.Show(row("DevbuzzID"))

When you access the ValueMember property you are really accessing the underlying datarow value of the data view.

Performance!!

The performance of this technique is pretty dismal when compared to the next technique I will show you and I would only recommend using it for prototype solutions such as a proof-of-concept type application. By the by, the double firing you see on the combo-box is the result of a beta 1 bug. I didn't use any work-around code since I want the attached sample code to be valid for the final release. Just add a variable to trap the first event.

Typically on my test device (Toshiba e570) I see an elapsed time of about 11-13 seconds to fill a combo-box with 500 rows using the bound data view technique.

Next Page