DataGridViews?

Vai

Vai

Soldato
Joined
18 Oct 2002
Posts
3,347
I am having problems working with DataGridViews in C# when I want to have it bound to Lists of objects.
Can anyone recommend a thorough guide on how to work with them in this situation? Possibly through a BindingSource? When googling the help I found was either "bind it to a database" or not very helpful.

Also I have seen this problem on two computers with Visual Studio. I make a blank project and put a DataGridView on the form, then add a DataGridViewComboBoxColumn and some other columns.
When I run the project the ComboBox needs to be clicked about three times before it drops down. The first time you click it nothing happens, the next time you click it the line between the bulk of the box and the down arrow disappears, then the next time you click it, it actually works :confused:

Perhaps someone could recommend a simpler alternative to a DataGridView? All I want to do is have a simple table like in Excel that I can populate with data and have an event fired when new data is entered :)
 
Last edited:
I also had trouble finding good information on how to use an ObjectDataSource against a DataGridView within WinForms (rather than ASP.NET to which most of the articles seem to apply) - particularly with respect to binding to a subset of columns (properties) on the underlying data type returned by the data source.

Do you have your DataGridView working with an ObjectDataSource yet? If not I can post an example if that helps.
 
I haven't tried using the ObjectDataSource class yet, any example you could provide would be great.
Unfortunately while DataGridViews seem to be very flexible it's taking me a while to get to grips with how to use it because every guide seems to use different methods and classes so I am not sure which is the right one for me to use :o
 
I've posted a *very* quick example WinForms project here that hopefully illustrates some really basic data binding of an object collection to a DataGridView (C# via Visual Studio 2005). Apologies if this is too basic and you've already covered this.

I did make a mistake with my terminology previously: an ObjectDataSource is specific to ASP.NET applications, whilst WinForms applications use a BindingSource. I haven't used an explicit BindingSource object in the example I posted, instead just binding directly to an object collection.

Hope this helps in some way.
 
Last edited:
Hi,

I have made a quick example to illustrate the problems I am having.
http://www.vai-net.co.uk/Example.rar

-When you run the program even though it is set to allowing adding new rows it doesn't give you the option to (no blank new line)

-If you add "_source.Add(new Row());" it doesn't like that at all and throws errors continuously.

-If you replace: this.Type.Items.AddRange(new object[] {
"High",
"Medium",
"Low"});
With: this.Type.DataSource = Enum.GetValues(GetType(Types));
As recommended by a few other websites it does not compile.

-If I replace it with:
this.Type.Items.AddRange(new object[] { Types.High, Types.Medium, Types.Low });
It does work but I can't change any fields without a "FatalExecutionEngineError" fatal error thrown.

-If I use a BindingSource, i.e.:
BindingSource bs = new BindingSource();
bs.Add(new Row());
dataGridView1.DataSource = bs;
It has two problems:
1. It isn't bound to _source any more, if I add "bs.DataSource = _source" you get the same problems.
2. You get the "click" problem I mentioned in the first post, where if you compile it and click the combobox nothing happens, you have to click it 3-4 times before the drop down list appears.
 
Last edited:
I've just skimmed your code as I'm not in a position to work with 2008, but what happens if you put in some default values for the Row class?
 
I uploaded the example made in VS2005 and added in some default values.
http://www.vai-net.co.uk/Example2005.rar

I realised that on the last step I had left "bs.Add(new Row());" in it which was causing the FatalExecutionEngineError.
However when I take it out the combo box magically becomes a text box :confused:
 
Last edited:
I have got everything working, apart from one thing.

I want to filter the results based on the enum (like having a "DoNotDisplay" type).
BindingSource does have a Filter property, but List’s don’t have a IBindingListView which I believe is what is needed for this (beyond my skill).

Is there another way to filter what is displayed in the DataGridView?
 
What did you change to get it working?

As you have pointed out, the List collection is a basic one and as far as I'm aware has no functionality to filter (unless you are using 2008 in which case, it might be possible with Linq, but I am far from certain!). To which end I would suggest a simple class; filtered list. This class will contain two List classes, one being the "master" set and one being the "filtered" set... and you iterate through the filtered set, removing ones that dont match the filter condition.

Thats how I did it with some file name filter and it was nice and rapid with around 300 items.

HTH :)
 
I wrote a class implementing IBindingListView to do filtering, but then found this implementation of it which is pretty impressive which does sorting as well:
http://sourceforge.net/projects/blw

I replaced my BindingSource with their BindingListView list and it all seems ok:
private BindingListView<Row> bindingListView = new BindingListView<Row>(original list);

Type.DataSource = Enum.GetValues(typeof(Types));
dataGridView1.DataSource = bindingListView;
bindingListView.ApplySort(TypeDescriptor.GetProperties(typeof(Row))["Type"], ListSortDirection.Ascending);

I am quite sad Microsoft haven't implemented IBindingListView in a List and DataGridViews could really do with some work done on them :p
 
Back
Top Bottom