none
Fill DataGridview created with designer RRS feed

  • Question

  • Hi

    I'm studing the use of DGV

    So in a Form I used drag-drop from toolbox for  2 DataGridView

    The first one DGV1 is empty (with no columns). In the second one DGV2 I have created 3 columns

    I have also used drag-drop from Toolbox for a BindingSource1

    The BindingSource1 has a bind to a Table (coming from Business Logical Layer ) of my application

    Then in the code I  bind  DGV1 to BindingSource1 and BindingSource1 to my Table

    Everything for DGV1 works well and the grid is filled with all data from table

    For DGV2 in the designer i set the DGV2 DataSource to BindingSource1 but the DGV2 is not filled with data

    I don't understand why

    Can You help Me ?

    This is the simple code I Used

    	Private Sub Apri_Click(sender As Object, e As EventArgs) Handles Apri.Click
    
    		UITable = BLL.FunGetTabella ' coming from BLL
    		BindingSource1.DataSource = UITable
    
    		DataGridView1.DataSource = BindingSource1
    
    end sub

    And this is the form

    Even if there is no code to fill DGV2, in the DataGridView Designer DGV2 has a bind to BindingSource1 but is not filled


    Tuesday, January 9, 2018 11:46 AM

Answers

  • If you read my original post about double columns in the edit part, I indicate to use 

    DataGridView2.AutoGenerateColumns = False to stop the extra columns from being created.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, January 9, 2018 1:38 PM
    Moderator

All replies

  • Hello,

    I advise to create the BindingSource in code then depending on if you want both DataGridView controls to be in sync or not use one or the other. Note that how I creates objects is gong to be different than yours, this is a simple demo.

    Out of sync

    Public Class Form1
        WithEvents BindingSource1 As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim UITable = New BLL.FunGetTabella
            BindingSource1.DataSource = UITable.UITable
    
            DataGridView1.DataSource = BindingSource1
            DataGridView1.DataSource = CType(BindingSource1.DataSource, DataTable).Copy
        End Sub
    End Class

    In sync

    Public Class Form1
        WithEvents BindingSource1 As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim UITable = New BLL.FunGetTabella
            BindingSource1.DataSource = UITable.UITable
    
            DataGridView1.DataSource = BindingSource1
            DataGridView1.DataSource = BindingSource1
        End Sub
    End Class

    When in sync if you move a row in the first DataGridView the row moves in the second DataGridView, out of sync, move a row in the first DataGridView the second does not move to the same row.

    Edit: If DataGridView2 has each column's DataPropertyName set all the above will work. If DataPropertyName is not set then three more columns will be added. To stop the creation of three more columns set each column's DataPropertyName then add a line of code as shown below.

    DataGridView2.AutoGenerateColumns = False


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, January 9, 2018 12:21 PM
    Moderator
  • Hi

    I'm studing the use of DGV

    So in a Form I used drag-drop from toolbox for  2 DataGridView

    The first one DGV1 is empty (with no columns). In the second one DGV2 I have created 3 columns

    I have also used drag-drop from Toolbox for a BindingSource1

    The BindingSource1 has a bind to a Table (coming from Business Logical Layer ) of my application

    Then in the code I  bind  DGV1 to BindingSource1 and BindingSource1 to my Table

    Everything for DGV1 works well and the grid is filled with all data from table

    For DGV2 in the designer i set the DGV2 DataSource to BindingSource1 but the DGV2 is not filled with data

    I don't understand why

    Can You help Me ?

    This is the simple code I Used

    	Private Sub Apri_Click(sender As Object, e As EventArgs) Handles Apri.Click
    
    		UITable = BLL.FunGetTabella ' coming from BLL
    		BindingSource1.DataSource = UITable
    
    		DataGridView1.DataSource = BindingSource1
    
    end sub

    And this is the form

    Even if there is no code to fill DGV2, in the DataGridView Designer DGV2 has a bind to BindingSource1 but is not filled


    Hi

    You say you added 3 columns to the DGV and the bound the datasource to it. That will cause 6 columns to be in DGV - the 3 blank ones you added manually and the 3 from the datasource.Try scrolling to the right and see the columns appended.


    Regards Les, Livingston, Scotland

    Tuesday, January 9, 2018 1:30 PM
  • Hi Karen

    i will be more clear

    I don't want to have 2  DGVs in the same form

    I wish have just one Dgv  and  create it with DGV Designer with the 3 columns.

    Then fill it with the data source

    If I do so, (even creating Bindingsource runtime)  and bind my DGV created with designer I get the result in the image, (double columns) 

    This is my DGV before running the application

    and this is the the way as the DGV is filled

    So double columns

    the code is simple 

    Private Sub Apri_Click(sender As Object, e As EventArgs) Handles Apri.Click
    		Dim BLL As New BLLNazioni
    		UITable = BLL.FunGetTabella
    		BindingSource1.DataSource = UITable ' binding source in the property (WithEvents BindingSource1 As New BindingSource )
    		DataGridView2.DataSource = BindingSource1
    
    		DataGridView2.DataSource = CType(BindingSource1.DataSource, DataTable).Copy
    
    
    
    end sub

    The Binding source is created Runtime.

    Tuesday, January 9, 2018 1:34 PM
  • If you read my original post about double columns in the edit part, I indicate to use 

    DataGridView2.AutoGenerateColumns = False to stop the extra columns from being created.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, January 9, 2018 1:38 PM
    Moderator
  • OK everything works

    Thank you

    Tuesday, January 9, 2018 2:57 PM