0

I drag and drop a DataSource on my winform which creates the DataGridView and corresponding BindingNavigator. Finally my .cs code file has just one line in Load() event:

userLogsTableAdapter.Fill(dataSetUserLogs.UserLogs);

Very neat! The problem is that one field of my UserLogs table is foreign key and I wanna show its description instead of the ID. I read many articles including this SO question but none is addressing this specific requirement using DataSource drag-drop method. I have to create many types of logs in my app. Can't afford to create DataSources and BindingNavigators from scratch in code for almost 25 types of logs. Kindly point me to the right direction.

Also how to filter the displayed data as discussed in the above-referred question.

Dia Sheikh
  • 190
  • 2
  • 16
  • In fact in both linked posts, using `DataGridViewComboBoxColumn` can be donecompletely using designer. Also take a look at [this post](https://stackoverflow.com/a/36476739/3110834) about combo column approach. – Reza Aghaei May 30 '18 at 07:54
  • I also went through the post that you marked my question as duplicate of, but none of the options discussed there target my scenario of having created the DGV using DataSource drag-n-drop method. I overrode the ToString() method to no effect for 2nd layer property display. – Dia Sheikh May 30 '18 at 08:21
  • Secondly, my scenario is just showing a listing of the log. No point in modifying the log. So combo box approach or saving data back to DB is not required. I just want to simply replace a column's content by its description from another table. That's it. – Dia Sheikh May 30 '18 at 08:25
  • 1
    `DataGridViewComboBoxColumn` is not just for editing. You can simply set its display mode to none, and also make it read only. As an example like your scenario, let's say you have a `List` Which each `Log` has a `LogTypeId` and you are going to show `LogType.Description` instead of `LogTypeId`. `DataGridViewComboBoxColumn` is exactly what you are looking for. It also can simply be done using `ToString` or `CellFormatting` approach. It seems linked posts contain all what you need. – Reza Aghaei May 30 '18 at 08:29
  • Hmmm... In that case, shall I have to convert the existing FK column to 'DataGridViewComboBoxColumn' through designer or create a brand new column? Secondly I don't have any 'List' but just designer-generated objects (of DataSet, BindingSource, TableAdapter, AdapterManager and Binding Navigator). Which one to assign to the 'DataGridViewComboBoxColumn' content? – Dia Sheikh May 30 '18 at 08:36
  • `DataSet/TableAdapter` is not EF 6 as you mentioned in title. – Reza Aghaei May 30 '18 at 08:39
  • But I had chosen "Entity Framework 6.x" when initially I had added database to my solution....? – Dia Sheikh May 30 '18 at 08:42
  • It seems you already have `DataSet` on your form and it means you are not using EF to achieve this. – Reza Aghaei May 30 '18 at 08:47
  • Kindly read my post again. I described the whole procedure of adding DataGridView on my form. I am using EF for modifications and additions (of single records) throughout my app. It's only now that I have switched to data retrieval in bulk. So may be the approach that I used (and described above) is not according to EF but that's how I am doing it. And my listing is showing quite nicely with minimum code required. I don't want to create from scratch as I have to replicate this method for some 20+ more types of logs. So I wanted shortest possible way of displaying the foreign key description. – Dia Sheikh May 30 '18 at 08:56
  • I reopened your post. But I'd say, It's unclear what you are asking because you can not say I'm using EF while you explicitly are saying you are using `DataSet` and `TableAdapter`. – Reza Aghaei May 30 '18 at 09:30
  • Ok, I corrected the title of the quest. Guide me as to what this approach that i am using (of dragging and dropping the Datasource onto WinForm to create corresponding DGV and BindingNavigator) will be called, because I saw this method in a tutorial on youtube which didn't name it. Since it worked for my case (when i am already using EF) so I naturally thought that this approach is according to EF. – Dia Sheikh May 30 '18 at 09:36

1 Answers1

0

Right now, I solved my problem by creating a view in database using join on both the concerned tables and simply creating a DataSource for that view rather than the original tables. But this was possible only because I was fortunately granted access to the database for creating the view. But I know there are scenarios where you can't get access to the DB server (for whatever reasons). So I would appreciate if someone posts a programatic solution here i.e. to achieve the same via designer/code.

I would be happy to mark that as answer.

Because this question is NOT a duplicate of the mentioned question. Although the requirement is the same but the underlying procedure being followed is quite different which makes them two different questions.

Dia Sheikh
  • 190
  • 2
  • 16
  • Are you using `DataSet` and `DataTable` or using Entity Framework and a `List` as data source? – Reza Aghaei May 31 '18 at 08:48
  • Neither. I actually followed this post (http://thedatafarm.com/data-access/using-entity-framework-entities-as-data-sources-in-a-winforms-app/) in essence for data binding. But that post is quite old (10+ years) so there are differences in how many and what objects and code are auto-created by designer. In my case 5 component objects (dataSet, bindingSource, tableAdapter, tableAdapterManager and bindingNavigator) are created and just one line of code (mentioned in OP). – Dia Sheikh Jun 13 '18 at 08:16
  • Read this post [What is the correct way to use Entity Framework as datasource for DataGridView?](https://stackoverflow.com/q/42114690/3110834) which is a really fact introduction to EF in Winforms. – Reza Aghaei Jun 13 '18 at 08:26
  • Also to implement filtering, take a look at this post [How to filter data using Entity Framework in a way that DataGridView be editable and context track changes?](https://stackoverflow.com/q/35977042/3110834) – Reza Aghaei Jun 13 '18 at 08:28
  • Then you will have a better understanding about solutions which I shared under the OP. – Reza Aghaei Jun 13 '18 at 08:31
  • I saw this article earlier but more complex than I need because I am only "listing" various types of logged events. I don't need to save the data back to DB. The article that I referred is extremely easy to follow and takes only 2 minutes to prepare your full form. Best for scenarios where lots of log views are to be created. But she discusses very basic points only. – Dia Sheikh Jun 13 '18 at 08:31
  • Ok. I think you deleted your comment to which I replied above. I'll read these articles and see where they fit in my case. Thanks a bunch! – Dia Sheikh Jun 13 '18 at 08:34
  • Yes, I could guess this, so I removed the link to [this](https://msdn.microsoft.com/en-us/data/jj682076.aspx) msdn article and shared other links. – Reza Aghaei Jun 13 '18 at 08:35
  • Also since you are not going to edit items, to show navigation properties, it's enough to shape the result using `var data = db.Log.Select(x=>new {Id= x.Id, ..., LogTypeName = x.LogType.Name}).ToList()` This is what I described in option 3 of [this post](https://stackoverflow.com/a/35117378/3110834). You simply can use anonymous type, you don't need a view model. – Reza Aghaei Jun 13 '18 at 08:38