BindingSource.ResetBindings and datagridview column-VBForums
Results 1 to 7 of 7

Thread: BindingSource.ResetBindings and datagridview column

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    BindingSource.ResetBindings and datagridview column

    I've got a datagridview DGV1 bound to source1. I've created source2 which is a single column (vendorcode) used to populate a combobox column of the datagridview.

    I've got another form in the project which populates a table from which the source2 (vendorcode) column comes from. This form uses a datagridview DGV2 bound to source3.

    When you enter a new row and update source3, I want the combobox column in DGV1 to be updated with the new row added to DGV2. I thought that source3.resetbindings would do it, but it is not.

    I hope I have explained this well enough.

    How can I get the combobox in DGV1 to be updated with newly added values while the program is still running?
    end war
    stop greed

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,157

    Re: BindingSource.ResetBindings and datagridview column

    So you're saying that your combo box column is bound to source2, your DGV2 is bound to source3, and source2 and source3 are both bound to the same DataTable, correct? If so then I don't see how you have a problem because the combo box column should update automatically. That's the whole point of data-binding. I just tested it to prove it to myself, and I've attached the project to prove it to you. Run the project and drop down the list. You'll see the names "Peter", "Paul" and "Mary". Now press the Button and add some more names to the list. Close the dialogue and drop down the list again. See how the names you added in the dialogue have been added to the drop-down list? There's not even any need to call ResetBindings.

    It's my guess that your source2 and source3 are actually not bound to the same DataTable. They are probably bound to two different DataTables that draw their data from the same source table in your database.
    Attached Files Attached Files

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    Re: BindingSource.ResetBindings and datagridview column

    mc,
    Your last statement I believe is correct. I have 2 datasets, each drawing off the same db table. I couldn't figure out how to name 1 column in the dataset as the datasource--I do everything with the designers because I don't have a full understanding of it all.

    I have several lookups like this and I got 2 to work. One isn't. I'll have to inspect further.

    Would you mind engaging in a conversation about your code? I learn easily by example but I'm not clear about a couple of things.


    Thanks for your help and awesome example.
    end war
    stop greed

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    Re: BindingSource.ResetBindings and datagridview column

    mc,
    Your last statement I believe is correct. I have 2 datasets, each drawing off the same db table. I couldn't figure out how to name 1 column in the dataset as the datasource--I do everything with the designers because I don't have a full understanding of it all.

    I have several lookups like this and I got 2 to work. One isn't. I'll have to inspect further.

    Would you mind engaging in a conversation about your code? I learn easily by example but I'm not clear about a couple of things.


    Thanks for your help and awesome example.
    end war
    stop greed

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

    Re: BindingSource.ResetBindings and datagridview column

    You can ask questions about my code here if you like. Anyone who wants to know what we're talking about can download the project if they wish.

    If you want to be able to set up all the data-bindings in the designer then your controls on different forms will be bound to different data. That's not ideal if you want changes on one form to be reflected in another.

    As you can hopefully see from the project I provided, if one form opens another it can simply pass the second form a DataTable, or even a DataSet, which you can then bind to the form's controls in code. The second form would already have a BindingSource for the purpose, as mine does, and so you simply bind the DataTable or DataSet to the BindingSource and then bind the BindingSource to the controls. You might have more than one BindingSource on the form, depending how many independent data-bindings you needed.

    The code to bind data to controls is slightly different, depending on the control. First of all, if you are passing a DataTable then you would assign the DataTable to the DataSource property of the BindingSource:
    vb.net Code:
    1. myBindingSource.DataSource = myDataTable
    If you're passing a DataSet then you can do this:
    vb.net Code:
    1. myBindingSource.DataSource = myDataSet.MyDataTable
    for a typed DataSet or this:
    vb.net Code:
    1. myBindingSource.DataSource = myDataSet.Tables("MyDataTable")
    for an untyped DataSet. You can also do this:
    vb.net Code:
    1. myBindingSource.DataSource = myDataSet
    2. myBindingSource.DataMember = "MyDataTable"
    for an untyped DataSet.

    That's got binding the data to the BindingSource covered. Binding the BindingSource to the control would be done like this:
    vb.net Code:
    1. myControl.DataSource = myBindingSource
    for a grid, like this:
    vb.net Code:
    1. myControl.ValueMember = "ID column name here"
    2. myControl.DisplayMember = "display column name here"
    3. myControl.DataSource = myBindingSource
    for a ListBox, ComboBox or combo box column in a grid, or like this:
    vb.net Code:
    1. myControl.DataBindings.Add("control property name here", myBindingSource, "data column name here")
    for simple controls like TextBoxes. Note that the control property name will usually be "Text" for a TextBox, but you can bind to almost any property you like.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    Re: BindingSource.ResetBindings and datagridview column

    Thank you, I'm trying to absorb all that. I really appreciate it. I have a couple of questions.

    #1
    You said
    If you want to be able to set up all the data-bindings in the designer then your controls on different forms will be bound to different data. That's not ideal if you want changes on one form to be reflected in another
    After the example you gave, I was able to bind 2 of the 3 lookup columns I've got going in my forms (don't know why 1 doesn't work) to datasets bound to controls on other forms. Why do you say this?

    #2
    I have only ever used datasets for any query--whether it be an entire table or results from a stored procedure, because I don't know how to do anything else with the designer. Is this wise? Is it expensive? Or when how does a DataSet compare to using a Datatable with regard to perfomance?


    #3
    Question about your code: how is it that you can say :
    Code:
    Me.Column2.....
    And there seems to be no other way to say it. How did Column2 become a property member of the form?
    end war
    stop greed

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,157

    Re: BindingSource.ResetBindings and datagridview column

    1. The whole point of data-binding is that the data source and the UI are bound together, i.e. what affects one also affects the other. You're asking how changes made on one form can affect another form. The answer is that both forms have to be bound to the same data source. That way what affects one form, i.e. changes made by the user, will also affect the data source, which will also affect the first form. If you have two data sources, each linked to a different form, then there is no relationship between the forms. By binding both forms to the same data you create that relationship.

    2. I have to say, I'm always amazed at the number of people who believe that they are using a DataSet and not a DataTable. That's like saying you're using a database but no tables. A DataSet is merely a container for DataTables and DataRelations, just like a database is a container for tables and relationships. Obviously a database can contain other types of things too, but the DataSet provides only a subset of the functionality of a database. If you get data from a database then you're populating a DataTable, whether it's in a DataSet or not.

    3. When you add controls and components to your form in the designer what's actually happening is the designer is writing code for you. If you add a TextBox to the form in the designer and set its Text property, the designer generates the code to declare a TextBox variable, create a TextBox object, assign it to the variable and set its Text property. When you add a DataGridView to your form something similar happens. When you add a column to that grid, the designer again generates the code to declare a variable, create an object and assign that object to the variable. 'Column2' in my code is that variable, which was declared when I added the second column to the grid in the designer.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

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.