DataGridView Almost Working....

B

bern11

I'm trying to bind a DataGridView to a BindingList that is a member of
the main form. It worked once, then stopped. Now it does not display
data when the list is clearly populated. I just get an empty grid.
There seems to be several ways to bind the grid, none of which seem to work:

1) I can set the DataSource to MainFormBingingSource and DataMember to
dataList,

-or-

2) I can set DataSource to MainFormBindingSource, then under it appears
an expansion where I can directly select dataList.

(MainForm is the form and dataList is the BindingList).

It works like a champ if I set DataSource = dataList in the form
constructor, but then I have to programmatically format it, uhggg!

So, what am I doing wrong? Why no data? Is there a 'Refresh Grid Data'
function?
 
B

bern11

PS. To make the dataList visible as a data source, I did have to
Refractor -> Encasulate Field it and promote it to a property (what a
cool feature!). Was that cheating and there is supposed to be another
way to make it visible...??? (As a simple member-variable, the list
could not be set as a DataSource or DataMember).
 
M

Marc Gravell

If you are using a BindingSource, then you do need to set the
MainFormBindingSource.DataSource (not DataMember) to the list, and
this is done at runtime. However, this doesn't mean you need to format
it manually. At design time, you should be able to bind to the Type
that you are going to display (i.e. the Order type) - this gives you
access to the columns etc for formatting; then simply set
MainFormBindingSource.DataSource = dataList (for instance in the Load
event / OnLoad override) and everything should work itself out.

Note, however, that the IDE is notorious for losing data bindings like
this ;-p My advice is to set it up how you want it, then snip the grid-
setup code from the MainForm.Design.cs (InitializeComponent) into a
private method in MainForm.cs - it might not appear at design-time,
but it won't get lost by the IDE either.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top