1

I have a DataGridView that is bound to a BindingList<T> of a custom business object, with fields DeckID, Name, UserID, Size, and Notes.

This is being called up from an MSSQL database via an IDataReader object. Now, my problem is that UserID is an integer that, in my database, is a foreign key to a table called SystemUser, with a Username field.
What I want to do is grab the Username from the SystemUser table in my database, and display that, based on whatever UserID, display that name. This was all very easy in WebForms, using a GridView, but not so much on a WindowsForms app.

Nanne
  • 64,065
  • 16
  • 119
  • 163
Jack
  • 950
  • 2
  • 17
  • 36
  • you should add a platform tag (silverlight, asp.net, winforms, etc) – Muad'Dib Jan 18 '12 at 18:00
  • You can write a custom query where you joining those tables. – pistipanko Jan 18 '12 at 20:45
  • True, but then I would have to create a new class to hold a specific query from SQL, and the data provider and controller for that class. I know it's possible to do this in Web Forms, so I'm all but certain it should be doable in Windows Forms. In my mind, this should be something that's fairly common... – Jack Jan 18 '12 at 20:57

1 Answers1

0

I think I see what you are looking for. From the DataGridView instance get the selected item using an event like onSelectionChange. Next use:

var selectedItem = datagridviewinstance.SelectedRows[x].DataBoundItem as YourTypeHere
var data = (from a in SOMETHING where fk == selectedItem.ForeignKeyProperty select a);
textbox1.Text = data.text;

Now you can make your next database call to retrieve the foreign key object and update whatever GUI object you wish. Good luck!

user959729
  • 1,127
  • 2
  • 12
  • 19
  • Sorry, I've only ever done stuff in C#...I don't know VB.NET at all, I don't understand what the second line says...but I knoe how to get databound objects from a GridView. I just don't understand how to get the DataGridView to automatically select the foreign table's data. – Jack Jan 18 '12 at 23:22
  • If you don't want to change your DTO to initially retrieve the data, you will have to make a second database call. Put an event on "Selection Change". There setup your database call using your foreign key. The datagrid will "Automatically select" the foreign key data when you change your selection. – user959729 Jan 19 '12 at 13:49
  • 1
    Sorry everyone, after some posts on the MSDN, they linked me back to an article here on StackOverflow, and the solution is here: http://stackoverflow.com/questions/3758757/c-sharp-datagridviewcomboboxcolumn-binding-problem – Jack Jan 20 '12 at 15:41