Click here to Skip to main content
15,881,687 members
Articles / Programming Languages / C#
Article

Yet another databound ListView (This time for .net2)

Rate me:
Please Sign up or sign in to vote.
3.35/5 (10 votes)
5 Mar 20063 min read 56.9K   1.7K   27   11
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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Co-director of Chabad Student Center @ SUNY New Paltz
Lecturer of Computer Science @ SUNY New Paltz

Comments and Discussions

 
GeneralMy vote of 1 Pin
adamvsmith14-Jun-10 8:01
adamvsmith14-Jun-10 8:01 
GeneralGood suff - here's a trick to make it a bit better! Pin
Dale Margel29-Apr-10 17:48
professionalDale Margel29-Apr-10 17:48 
GeneralRe: Good suff - here's a trick to make it a bit better! Pin
Rabb Moshe Plotkin30-Apr-10 2:18
Rabb Moshe Plotkin30-Apr-10 2:18 
B"h

I haven't used .net2 in years.
WPF which was released with .net3.5 has turned every control into a template based control.
Its almost like the guys at microsoft read this article and said "Hey we can do this for every control."
Thats all I use now days. so I havent looked at this for ages.
GeneralRe: Good suff - here's a trick to make it a bit better! Pin
Dale Margel4-May-10 7:36
professionalDale Margel4-May-10 7:36 
Generalorder Pin
durbich12-Apr-10 4:26
durbich12-Apr-10 4:26 
Generalorder problem Pin
durbich12-Apr-10 4:25
durbich12-Apr-10 4:25 
GeneralRe: order problem Pin
Rabb Moshe Plotkin30-Apr-10 2:17
Rabb Moshe Plotkin30-Apr-10 2:17 
GeneralYou forgot to unsubscribe to the old data source event handler ! Pin
lcalabrese9-Dec-08 18:54
lcalabrese9-Dec-08 18:54 
QuestionBind Columns? Pin
mcgrath13-Jun-07 10:03
mcgrath13-Jun-07 10:03 
AnswerRe: Bind Columns? Pin
Boinst20-Sep-07 20:45
Boinst20-Sep-07 20:45 
GeneralExcellent! Kol Hakavod! Pin
Simmy78-Jun-06 21:59
Simmy78-Jun-06 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.