none
Databound ComboBox changing the selection on a DataGridView RRS feed

  • Question

  • First of all, i would like to apologize if this isn't the correct place for this question but this was my best guess.

    I have a database table that has a recursive relationship with itself. It's a Products table, and needs to be recursive because some products are really just a combination of other products.

    I've got two DataGridViews and a combo box to handle data manipulation, all successfully bound and working perfectly.

    The problem comes when changing the selection of the combo box. It, as well as one DataGridView are bound to the same DataTable. Whenever i change my selection in the combo box, the corresponding row of the dataGridView also gets selected automatically.

    It looked like a nice bonus at first, but now i realize that this automatic changing of selection is interfering with my idea.

     

    The basic workflow of this form should be:

    1) User selects a product from DGV1

    2) DGV2 shows all (if any) components of the product currently selected in DGV1

    (*it works perfectly up until this step)

    3) User selects a Product from the combo box and clicks the ADD button, which adds the selected Product as a component to the Product currently selected in DGV1

    Step 3 never works as planned, because as soon as i change the combo box selection, DGV1's selection also changes, and so i can no longer tell which product the user selected in the first place.

     

    Again i'm sorry if this doesn't belong here. I'm guessing that the cause is having both DGV1 and the combo box bound to the same DataTable, making it a DataTable/DataSet issue, but i'm not sure.

    I see no other way to implement the functionality i desire, and so would like to know if i can prevent a DataGridView from automatically selecting it's rows depending on user input in other controls.


    Thursday, October 6, 2011 2:22 AM

Answers

  • Yes, the problem is that you can't use the same DataSource for a ComboBox and any other control. The ComboBox.DataSource has to be unique. Now, that said, it *can* be the exact same DataTable (you don't have to Clone it as James suggested), as long as you use two different BindingSources. Try this:

    // notice they are the SAME DataTable
    BindingSource bsGrid = new BindingSource();
    bsGrid.DataSource = MyDataTable;
    
    BindingSource bsCombo = new BindingSource();
    bsCombo.DataSource = MyDataTable
    
    DGV1.Datasource = bsGrid;
    CboBox1.DataSource = bsCombo;
    
    

    Rows added (or any other changes) in the grid will show up in the Combo.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, October 9, 2011 12:51 AM

All replies

  • Why not just clone the DataTable and set the ComboBox datasource to this?

    (untested)

    DGV1.Datasource = origDataTable
    
    CboBox1.DataSource = origDataTable.Clone
    
    
    

     


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions
    Thursday, October 6, 2011 2:28 PM
  • thanks james.

     

    This occurred to me yesterday after making my first post. It works, but the problem is that if i add new rows to DGV1, i would like to have them appear in the combo box as well. If i use two different data sources, changes to the first wont appear in the second.

    If i did this i'd have to clone origDataTable and assign it to the combo box EVERY time a product was added, changed or removed. I'd really like to avoid as much of this "patchy" coding as possible. I've restarted this whole project form scratch because of messy code.

    Thanks for your input. I really appreciate it.

    Thursday, October 6, 2011 9:10 PM
  • thanks james.

     

    This occurred to me yesterday after making my first post. It works, but the problem is that if i add new rows to DGV1, i would like to have them appear in the combo box as well. If i use two different data sources, changes to the first wont appear in the second.

    If i did this i'd have to clone origDataTable and assign it to the combo box EVERY time a product was added, changed or removed. I'd really like to avoid as much of this "patchy" coding as possible. I've restarted this whole project form scratch because of messy code.

    Thanks for your input. I really appreciate it.


    Honestly, I think your problem, as with most of similar types of issues I see, stems from a poorly designed database.  I have several implementations that manage a multitude of databases containing many tables and millions of rows of data... Not one instance I can think of where I need to share a dataTable between a DGV and a Combobox.

    Focus on properly normalizing your database and these kinds of application tier issues tend to fade away or never even show up in my experience.  In fact, I'd recommend you don't even write a single line of code for the application tier until you really nail down the architecture of your data store first -- that is just my own opinion though, which has actually served me very well when I have followed it.


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions
    Thursday, October 6, 2011 9:42 PM
  • Yes, the problem is that you can't use the same DataSource for a ComboBox and any other control. The ComboBox.DataSource has to be unique. Now, that said, it *can* be the exact same DataTable (you don't have to Clone it as James suggested), as long as you use two different BindingSources. Try this:

    // notice they are the SAME DataTable
    BindingSource bsGrid = new BindingSource();
    bsGrid.DataSource = MyDataTable;
    
    BindingSource bsCombo = new BindingSource();
    bsCombo.DataSource = MyDataTable
    
    DGV1.Datasource = bsGrid;
    CboBox1.DataSource = bsCombo;
    
    

    Rows added (or any other changes) in the grid will show up in the Combo.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, October 9, 2011 12:51 AM