Results 1 to 3 of 3

Thread: [RESOLVED] Databound Datagridview question

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Resolved [RESOLVED] Databound Datagridview question

    Can anyone tell me why "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound." I keep getting an error "Invalid Operation Exception was Unhandled" error.

    All I am attempting to do is allow the user to select products from a list(or a second datagridview) and have it added to the datagridview so they do not have to type in every cell since the other cells are not visible because they do not purtain to their needs. Plus they will be able to save all items at the same time.

    I was able to get it to accept the first row and then it errors when adding a second row. I tried using the following line:
    Code:
     DataGridView1.Rows.Add()
    This above line of code would run when or if another item was selected. I was hoping this would insert a new line so that the next item selected could be placed in it but this is when the error occurs. I even tried the following;
    Code:
            DataGridView1.Rows.Add(New String() {VendorIDLabel1.Text, _
                                                                ComboBox1.Text, _
                                                                TextBox1.Text, _
                                                                TextBox2.Text, _
                                                                TextBox3.Text, _
                                                                TextBox4.Text, _
                                                                CommLabel.Text, _
                                                                TaxCollectedLabel1.Text, _
                                                                ManagerIDTextBox.Text, _
                                                                ManagerNameTextBox.Text, _
                                                                DateLabel1.Text})
    Same error occured. If anyone has any suggestions, I am all ears. I'm at my wits end on this one. If more info is required, let me know.

    PS. to answer the question you may be asking yourself is why am I doing it this way, easiest way to explain it is to imagine having multiple stores in one place all using the same teller. Each store has there own product database. Instead of having multiple receipts, I am trying to store all sold products in its own table so I can keep track of what each vendor sells and so I can print all items on 1 receipt.

    Sorry the the long winded explaination.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Databound Datagridview question

    Because, when the grid is bound, it displays the contents of the DataSource. If you want to adda row to the grid then you need to add a new item to the DataSource. The grid will then update accordingly.
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Re: Databound Datagridview question

    Thanks jmcilhinney, that makes perfect sense. Instead of manipulating the datagridview, I should be manipulating the datasource. I fiqured there was a logical explanation, I just couldn't think of it.

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
  •  



Click Here to Expand Forum to Full Width