65.9K
CodeProject is changing. Read more.
Home

Yet another databound ListView (This time for .net2)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.35/5 (10 votes)

Mar 5, 2006

3 min read

viewsIcon

57853

downloadIcon

1745

Inherits from the ListView control and adds the functionality of allowing you to easily add a DataSource using .net2s BindingSource

Sample Image - DBListView.jpg

Contents

Introduction 

This control like so many before it, inherits from the ListView control and adds the functionality of allowing you to easily add a DataSource from which the list will be built.

If so many before me have built the same control, why you may ask am I reinventing the wheel?

Good question! And I have an equally good answer.

Because I am the first one to do it in .net2!

And why do I need to make one just for .net2? Won’t the earlier ones still work?

Yes they will and quite well. Just not as good as a true .net2 control will.
You see, Microsoft totally redid the DataBinding in .net 2(which shipped with vs2005). Everything is based around a new (non visual) component call the BindingSource for windows forms based programs and DataBindingScource for web based programs.

While the young and sexy asp.net v2 has been getting all the attention. Her older plainer sister Windows Forms has also learned a few new tricks.
This new BindingSource accomplishes a lot of cool things. But the best in my opinion is how much it hides what the data source is. To the control there is no difference whether the data is coming from a database through a DataSet, from a WebService through a custom class or from an operating system function in the form of an array of COM classes.

So creating a ListView that works off of the BindingSource is a major accomplishment. It was also extremely easy; the entire project is less than 4k! And a great opportunity for everyone to see what it takes to create a control for the new BindingSource.

The control is bare bones and I will be updating it shortly.

When I am done I will post another article explaining the interesting points of development.

In the mean time I am only posting a short article on how to use it.

Background 

Anyone who does database front ends in windows with .net will find this indispensable.

Using it couldn’t be easier! Take a look at the demo!

Anyone who knows the basics of C# or VB.net will find it a sinch.

Using the code 

Its quite easy. To use the control you need to add it to your form as you would any other control.

That is you either add it in code. Or if you add the project containing it to your solution it will showup right in your toolbox and then you can just drag it onto the form.

Once the control exists its just a mater of creating a BindingSource with your data (of almost any kind) as its DataSource and then assign it to the controls DataSource!

This can be done either by creating the binding source in code such as

DBListView1.DataSource = new BindingSource(MyData,””);

Or graphically by adding a BindingSource to your form and choosing it as the DataSource for your control in the property inspector.

And then if you need to, you can change the data in the DataSource of the BindingSource as I do in the demo project.

bindingSource1.DataSource = new System.Drawing.Text.InstalledFontCollection().Families;

Have Fun!