Winforms Sample

Jan 17, 2010 at 2:31 PM

Any chance of a sample Winforms app?

I'm trying to get a datagridview to display data using 'dataGridView1.DataSource = CustomerCollection.RunSelect();' but nothing is been displayed.

Hopefully I'm missing something really obvious.



Jan 18, 2010 at 1:46 AM

Ok there are a couple of things here. First you will need to create bindable columns get it to work. The only way I could get it to work was like the code below. I had to create a binding list for my Customer collection. Also even if you do this, it still will not work until you download the latest version and regenerate. The grid will not display items unless they are marked browsable. By default all fields were marked false. So even if you run the code below, there will be no columns displayed with you generator version. Thsi was a vestige of some old WinForms generators that we used to distribute. We actually had generated WinUI and you could mark the fields you wanted in grids. This is not public anymore so now all fields are marked as browsable and you can control which are shown by defining the fields you want as in the code below. Please download and regenerate.

DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.DataPropertyName = "FirstName";
column1.HeaderText = "First Name";

DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
column2.DataPropertyName = "LastName";
column2.HeaderText = "Last Name";


CustomerCollection customerCollection = CustomerCollection.RunSelect();
var newList = new BindingList<Customer>();
foreach (Customer item in customerCollection)

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = newList;

ataGridViewTextBoxColumn column1 =


Jan 18, 2010 at 5:56 PM
Edited Jan 18, 2010 at 5:57 PM

Thanks for the prompt response.

That would explain why the Customer class wasn't showing any columns in the datasource on the designer.

Columns are now displaying OK.

Jan 28, 2010 at 10:44 AM

Would it be possible to write your own "ToDataTable()" function? How would I do such a thing?

I looked around in the generated code, but I couldn't find the code for the functions of "ToArray<>" anywhere.

Jan 28, 2010 at 4:45 PM

Please tell me what you are trying to accomplish. Why do you need a datatable? All information is structured in objects. It is all strongly-typed and bindable if that is your concern. If you let me know what you are trying to do I can help you with the API. Also ToArray<> is a generic function that you can call on any IEnumerable collection. Make sure you have System.Linq in your using statements.

This is standard syntax and it does work.

CandidateCollection candidateCollection = CandidateCollection.RunSelect();
Candidate[] array = candidateCollection.ToArray<Candidate>();

Jan 29, 2010 at 7:48 AM
Edited Feb 1, 2010 at 7:00 AM

I'm trying to bind the result from RunSelect() to a DataGridView. And I'd like to use a nice statement .ToDataTable() (or an .ToArray<Something>) to fill up my DataGridView. A the moment I've added this code to CategoryCollection.cs file. But the problem is that I run through my result twice when I try to search the database. So when I use" CategoryCollection.RunSelect().ToBindingList()", it works nicely. but when I try "CategoryCollection.RunSelect(p => p.Description.StartsWith("S")).ToBindingList()", it doesn't give me back anything.

public BindingList<Category> ToBindingList()
BindingList<Category> list = new BindingList<Category>();
foreach (Category cat in CategoryCollection.RunSelect())
return list;

Jan 29, 2010 at 1:00 PM

Are you sure that the collection has 0 items in it. IT may be something else. First make sure you have a category object in the database that starts with "S". I have done this and it does pull back the desired elements. Next the code above for ToBindingList make check to determine if you are looping and actually adding elements to your binding object. Also your method is an instance method on an existing collection; however you are pulling even more category objects by using the static select all method. You will hit the database twice. That collection has presumably been loaded already. Simply loop through the "this" variable. Also the LIQN syntax for starts with should work. As I have said I just tested it. You can use the SQL profiler to see exactly what is being run against the database. My guess is that either you are pulling back data and the grid does not have columns defined or their are no categories that start with "S". Please use SQL profiler to verify what is being queried and add a break point to your loop above to verify that objects are being loaded from store.

Jan 29, 2010 at 1:10 PM

I wrote this code on the CategoryCollection customization class.


public BindingList<Category> ToBindingList()
BindingList<Category> list = new BindingList<Category>();
foreach (Category item in this)
return list;

This worked fine. I have a Windows form with this code.


CategoryCollection categoryCollection = CategoryCollection.RunSelect(x=>x.Name.StartsWith("b"));
dataGridView1.DataSource = categoryCollection.ToBindingList();

CategoryCollection categoryCollection = CategoryCollection.RunSelect(x=>x.Name.StartsWith("b"));
dataGridView1.DataSource = categoryCollection.ToBindingList();

Notice I used the Lambda syntax and I see one item in my grid.




Feb 1, 2010 at 7:26 AM

Thanks. The problem was that I ran my select twice. Once with the where clause and once without it, and that gave a problem. I needed to know how to cycle trough my collection without doing a RunSelect() again. For some reason it didn't give anything back. That's solved now. Thanks a lot!

Jun 26, 2010 at 11:14 PM

I am seriously late on this discussion but I have just started looking at nHydrate and have started to play with it in a winforms context.

To be fair I am very new to Winforms so not all that up to speed with how databinding works with datafields.

I have read the above and it reads to me as though the objects created by nHydrate can not be used directly as datasources for things like the datagridview control and other databound controls. If so this means that I have to write code to bind the data to the control at run time which precludes the ability to set up the datagridview columns (for example) at design time. Is this correct or am I missing something here?