1
votes

Although there are a lot of posts on this site about populating a gridview with objects, I can't get it working.

I have a class called Logs with 3 public properties - Time, Description and Error. There is also a public property called logList that will return a List of Logs Objects.

And I have DataGridView in my WinForm, called myGV, with 3 columns called Time, Description and Error.

So I'm trying:

myGV.DataSource = Logs.logList.OrderBy(x => x.Time);

But my DataGridView displays nothing, even though logList does contain data.

Thanks for your work on this site!

UPDATE: If I remove all columns from myGV it does display data. So how do match static columns to the properties in my List of Objects?

3
How are your columns "called" something?Henk Holterman
Also, are you using WinForms or WPF?JFTxJ
The column's 'Name' property is set to Time, Description and Error.Cameron Castillo
What type is your logList? Is it a List<>, Array, ...? (Even better, can you please post your Logs class?)JFTxJ
Take a look at this exemple if it can help you setup the binding differently: stackoverflow.com/questions/125109/… (Take a look at the answer by Jared)JFTxJ

3 Answers

3
votes

It's strange that you said If I remove all columns from myGV it does display data.... I reproduced your problem and the reason is your LINQ query is not executed. You have to call ToList() or similar method before using the result as DataSource of your DataGridView:

myGV.DataSource = Logs.logList.OrderBy(x => x.Time).ToList();

Of course, If your static columns don't have DataPropertyName matched with the properties of the DataSource, there will be more columns added to your DataGridView than you expect. For example, suppose all the Time, Description and Error are added at design time without assigning any DataPropertyName and yourDataGridView.AutoGenerateColumns = true (by default), if you assign the DataSource of your DataGridView as above, your DataGridView may have 6 columns at all, instead of 3. So you can assign the DataPropertyName of your added columns before assigning the DataSource for your DataGridView, something like this:

myGV.Columns["Time"].DataPropertyName = "Time";
myGV.Columns["Description"].DataPropertyName = "Description";
myGV.Columns["Error"].DataPropertyName = "Error";
myGV.DataSource = Logs.logList.OrderBy(x => x.Time).ToList();//This should be called at here after all the DataPropertyNames are initialized.

I recommend you to set myGV.AutoGenerateColumns = true (by default) and remove all the added columns, just let the DataGridView auto-generate columns for you.

3
votes

After you manually add the columns, you have to make sure you update the DataPropertyName of each one of them:

  • Click in the little arrow on top of your DataGridView, to display the Control's Tasks. enter image description here
  • Click on Edit Columns.
  • For each of your columns find the property DataPropertyName and change them to Date, Error and Description and whatever other names you have used for your columns. enter image description here

Make sure these match with your Properties in your Logs class.

enter image description here

0
votes

OrderBy() returns IOrderedEnumerable<> type of data, that are not bindable to DataGridView. So you have to cast them to a Binding Source. Use ToList() method like "OrderBy().ToList()" to bind your gridview. dataGridView1.DataSource = studList.OrderBy(a => a.Age).ToList();

It is working Screenshot

For more info to bind DataGridView visit dataGridView binding