VS 2012 Datagridview from Linq to SQL-VBForums
Results 1 to 12 of 12

Thread: Datagridview from Linq to SQL

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    24

    Question 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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,507

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    24

    Re: Datagridview from Linq to SQL

    Thanks Paul, but if you would give me any example it would be better for me.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,507

    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

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,049

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,049

    Re: Datagridview from Linq to SQL

    Quote Originally Posted by LeoVB View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    24

    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 !!!

    Name:  DGV.jpg
Views: 762
Size:  81.1 KB

    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.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,507

    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

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,049

    Re: Datagridview from Linq to SQL

    Quote Originally Posted by LeoVB View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    24

    Re: Datagridview from Linq to SQL

    Quote Originally Posted by jmcilhinney View Post
    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.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,321

    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.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    24

    Re: Datagridview from Linq to SQL

    Thanks !!!

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.