-
Jan 24th, 2014, 04:41 PM
#1
Thread Starter
Junior Member
Datagridview from Linq to SQL
Hi
I need to populate a DataGridView from 3 tables but the problem is that one of the cell (column) of DGV is a ComboBox to show various values in it. I could not find any solution.
Is there any way to build a query (Linq to SQL) that meets what I need?
Should I use a DataSet or DataTable or BindingSource or non of them?
Thanks a lot
-
Jan 24th, 2014, 05:01 PM
#2
Re: Datagridview from Linq to SQL
to populate your dgv comboboxes, you need to either add the items to the datagridviewcomboboxcolumn, or set a datasource for the datagridviewcomboboxcolumn.
you then bind the dgv as you would if there was no datagridviewcomboboxcolumn + it'll select the correct combobox item for each row
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 24th, 2014, 05:15 PM
#3
Thread Starter
Junior Member
Re: Datagridview from Linq to SQL
Thanks Paul, but if you would give me any example it would be better for me.
-
Jan 24th, 2014, 05:43 PM
#4
Re: Datagridview from Linq to SQL
ok. this is the simplest example I could think of:
Code:
Public Class Form1
Dim r As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strings() As String = {"one", "two", "three"}
'this populates the ComboBoxes
DirectCast(DataGridView1.Columns(1), DataGridViewComboBoxColumn).DataSource = strings
'this creates the datasource for the dgv. it's just numbers 1 to 10 + random combobox values
Dim dt As New DataTable
dt.Columns.Add("C1")
dt.Columns.Add("C2")
For x As Integer = 1 To 10
dt.Rows.Add(x.ToString, strings(r.Next(0, 3)))
Next
'as i added the columns at designtime, i don't want columns created
DataGridView1.AutoGenerateColumns = False
DataGridView1.Columns(0).DataPropertyName = "C1"
DataGridView1.Columns(1).DataPropertyName = "C2"
DataGridView1.DataSource = dt
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 24th, 2014, 10:35 PM
#5
Re: Datagridview from Linq to SQL
You might like to follow the CodeBank link in my signature below and check out my thread on A ComboBox Column In A DataGridView. It provides step-by-step instructions on how to build a working example.
-
Jan 24th, 2014, 10:39 PM
#6
Re: Datagridview from Linq to SQL
Originally Posted by LeoVB
Hi
I need to populate a DataGridView from 3 tables but the problem is that one of the cell (column) of DGV is a ComboBox to show various values in it. I could not find any solution.
Is there any way to build a query (Linq to SQL) that meets what I need?
Should I use a DataSet or DataTable or BindingSource or non of them?
Thanks a lot
What exactly do you mean by populating it from three tables? You say that you have one combo box column so it that one of the tables? A more detailed explanation is in order. Each of your LINQ to SQL entity classes represents a single table in the database. If you want to display combined data in your application then you have two main choices:
1. Create a view in your database and then generate another entity class from that.
2. Write a LINQ query that joins two or more tables and then use the result of that.
In both cases, you're not going to be able to update your database directly, so you'd have to copy data manually to your original entities to save.
-
Jan 25th, 2014, 05:31 PM
#7
Thread Starter
Junior Member
Re: Datagridview from Linq to SQL
Hi Jmcilhinney
Let's see.
We have two tables with a many-to-many relationship through a third table between PRODUCTS table and CATEGORIES table. So, one product may have zero, one or more categories, and the same way, one category may have zero, one or more products.
The only I need is a DataGridView showing each product by row (as normal) and one of these columns have to be a ComboBox ir order to show every category that belongs to each product.
Please, see the attached file !!!
If you would make some real example and attach it to this post, it would be great.
Thanks
Last edited by LeoVB; Jan 25th, 2014 at 05:47 PM.
-
Jan 25th, 2014, 05:43 PM
#8
Re: Datagridview from Linq to SQL
I showed you how to set a datasource for the column + jmcilhinney directed you to an example.
You should be able to take it from there yourself
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 25th, 2014, 10:43 PM
#9
Re: Datagridview from Linq to SQL
Originally Posted by LeoVB
Hi Jmcilhinney
Let's see.
We have two tables with a many-to-many relationship through a third table between PRODUCTS table and CATEGORIES table. So, one product may have zero, one or more categories, and the same way, one category may have zero, one or more products.
The only I need is a DataGridView showing each product by row (as normal) and one of these columns have to be a ComboBox ir order to show every category that belongs to each product.
I'm not sure that you've thought that through. A ComboBox shows a single value selected from a list. If you have one row for each Product and a list of Categories in the ComboBox column then you can only show one Category per Product. If you want multiple Categories displayed for a Product then you need something that can display multiple records, which excludes a ComboBox. If you want to display the Products in a DataGridView then you might want to consider displaying the Categories in a separate control that can display multiple selections, e.g. another DataGridView, a ListBox or a CheckedListBox.
-
Jan 26th, 2014, 12:20 PM
#10
Thread Starter
Junior Member
Re: Datagridview from Linq to SQL
Originally Posted by jmcilhinney
I'm not sure that you've thought that through. A ComboBox shows a single value selected from a list. If you have one row for each Product and a list of Categories in the ComboBox column then you can only show one Category per Product. If you want multiple Categories displayed for a Product then you need something that can display multiple records, which excludes a ComboBox. If you want to display the Products in a DataGridView then you might want to consider displaying the Categories in a separate control that can display multiple selections, e.g. another DataGridView, a ListBox or a CheckedListBox.
Of course a ComboBox shows one Category per product. That is what I want. In the example above I just wanted to show you what items could have inside the ComboBox. So, if you want to see every Categories that product belongs to, you have to click on that ComboBox to display categories. That's all.
-
Jan 26th, 2014, 01:05 PM
#11
Re: Datagridview from Linq to SQL
Hello,
.paul. and JMC gave you want is needed, here is a working example were I populate one ComboBox column then when a cell value changes show how to get the changed text for that ComboBox column cell and then (not needed for you) set another column based in that selection. This example is but one method to do this and don't get hung up on where the data comes from but in the end we have a populated DataGridView. Lastly, I am using a BindingSource which provides additional functionality then going with just a DataTable.
-
Jan 28th, 2014, 08:30 AM
#12
Thread Starter
Junior Member
Re: Datagridview from Linq to SQL
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|