DataGridView Second Level Property / Foreign Key Property - DataSource Binding

0
0

Hello IceTeaGroup.

I got a problem an I hope you might be able to help me with this. I got a DataGridView and set it’s DataSource to our main table.

 

DataBaseEntities context = new DataBaseEntities();

var y = context.tbl_MainTable.OrderByDescending(p => p.rowID).Take(50).ToList();

MyBindingSource.DataSource = new ObservableCollection<tbl_MainTable>(y);

 

So far, so good.

Within the MainTable we got references / foreignkeys to a few other Tables. What I like to do now is, to bind a DataGridView Field to a property of a referenced table.

As an example. In the MainTable is a product field. This field holds the value of the foreignkey to our ProductTable with a ProductID field and a ProductName field. How could I achieve that the DataGridView shows the ProductName value instead of the ProductID?

And is it possible to do it with third or fourth level properties?

 

Kind regards

MK

  • You must to post comments
0
0

Thank you for your answer.

The way to use a Combobox to show plain text was one I have tried but didn’t get what I want. I would like to have only one DataSource for the DataGridView, just because this DataSource got all values I need. I got all my references and all my data that is respond to the plain data that is shown in the DataGridView. And because I wanted to use DataBinding I wondered if there is no way to get second Level data.

I thought about an option in the designer to set a DataBinding like. “DataPropertyName: tbl_Preisarten.Preisart”

 

Maybe the added picture will help. You see an object of our table with all references and we also get all data from the other tables.

Kind regards

Attachment
  • You must to post comments
0
0

Hi,

You can get this behavior in several different ways.

If you want multiple values to show in one field, you can use a data-bound ComboBox DataGridViewColumn.  You can set the DataSource of the ComboBox to be whichever table you desire.

 

The second way is you can use LINQ to get the data from your second table and join it with the first.  You can select the fields to hide/show that way by adding them to your BindingSource.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/join-clause

 

I hope this helps.  Please let me know if anything doesn’t make sense or you have any other questions!

Best regards,

Levie

  • consystems
    Sorry, I posted a answer instead of adding a comment to your post.
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.