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 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
 
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 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?
 
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