July 2006 - Posts

Updating the data of a data bound control.

This is an update to a previous post I made in another blog. It's to do with a problem in data binding thats not immediately obvious. Say you have a TextBox control and the Text property is bound to a field in some data structure.

If you set the TextBox.Text property through code like this.....

        Me.TextBox.Text = "Hello World"

...then the text in the TextBox is updated however the underlying data isn't. The TextBox displays one thing but the bound data stores something else and it's not that obvious there is a difference. 

You need to update the value right after setting the text using the following...

        Me.TextBox.DataBindings.Item("Text").WriteValue()

...this is a much simplier approach than the one I last posted.

Posted by dsmyth

A class to export DataView to Excel *fixed*

A while back on my previous blog I wrote an entry about how to export data to Excel using ADO.NET.  It used SQL commands to create the spreadsheet and then used SQL INSERT commands to export the data .  The sample code on that post was only written to document the approach so that  I wouldn't forget it. 

I used this technique in a rewrite of a stress calculator. The application had to process an extremely large amount of raw data stored in a number of different Excel spreadsheets. The application did some heavy calculations on the data and then exported all the results to another Excel spreadsheet. Prior to the rewrite the export used OLE automation and took maybe 20 minutes to complete, after the rewrite took roughly 15-30 seconds.

The code was originally written in such a way that it was specific to the data it exported, not good, but yesterday I had the chance to polish it up and to abstract it out so that it could be used by any project. You'll find it attached to the post.

All you need to do is call....

               ExcelExporter.Export(myDataView, mySpreadsheetPath)

*update 18th July 2006, turns out there was a bug in the class. It has been fixed! The problem was the class only handled System.Double and System.Integer number types and didn't include System.Int16, System.Int32, and System.Decimal. Everything's working fine with it now.*

If anyone finds problems with the code I post then please do tell me so I can fix it.

Posted by dsmyth
Filed under:

First look at Generics - Array.ForEach(Of T)

Had a look at generics today and I'm very impressed. Here's an article from MSDN Magazine that covers what generics are all about.

Here is some code that demonstrates the Array.ForEach(Of T) generic loop. The code uses the DataTable.Select() method to pull out an array of DataRows and then, using Array.ForEach(Of T),loops over the rows performing some action. The action is an Action(Of T) delegate.

The code below performs the for each loop over an array of STypedTableRow elements calling the delegate DeleteSTypeRow.

Array.ForEach(Of STypedDataSet.STypedTableRow) _

         (STypedDataSet.STypedTableRow.Select("[ID] = '" & Me.IDTextBox.Text & "'"), _

         New Action(Of STypedDataSet.STypedTableRow)(AddressOf DeleteSTypeRow))

The method below is called for each item. There is no need to pass the current STypedTableRow object to the delegate, it's handled by the CLR.

Private Sub DeleteSTypeRow(ByVal obj As STypedDataSet.STypedTableRow)

               obj.Delete()

End Sub

Posted by dsmyth
Filed under:

Changing the formatting of bound data.

I've never really been a fan of data binding. I never really liked it because it removed a certain amount of programming control over displaying data and made certain things more difficult than they should be. Like, for example, changing the format of the data being displayed.

NET v2.0 databinding has changed my mind though, it's far more powerful and flexable, however the same problem occurs. How do you format the data being displayed if you don't have control of the displaying of data.

For example, a textbox who's text property is bound to a decimal field called RetailPrice displays for each record a value formated as 000.0000.... but how do you format the data so that it's displayed in the format £0000.00?

Here's how..........

Me.RetailPriceTextBox.DataBindings.Item("Text").FormatString = "£###0.00"

translates to change the value bound to the text property of the RetailPrice textbox to display a string in the format £###0.00...

Posted by dsmyth
Filed under:

creating DISTINCT values from a DataTable

First of all hello, this my first post here so be prepared for some bad grammer.

I'm working on an application right now that stores data in a dataset. The dataset works like a local cached copy of a portion of a larger SQL Server database. The data is downloaded into the application where the user works on it. The user then can upload the data back into the database whenever they are ready. All the data is stored and accessed locally.

On one of the applications forms I need a combo box that lists all the unique values of a datatable. I need to run a DISTINCT query on a datatable and bind the combo box to the results.

Me.ComboBox.DataSource = DataSet.DataTable.DefaultView.ToTable(True, New String() {"column"})

Me.ComboBox.DisplayMember = "column"

The DataView.ToTable() method returns a DataTable, True specifies that the DataTable will contain distinct values, while the string array contains the columns in the DataTable.

Posted by dsmyth | 1 comment(s)
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.