VS 2013 Select multiple rows from DataGridView and save them to DataTable-VBForums
Results 1 to 3 of 3

Thread: Select multiple rows from DataGridView and save them to DataTable

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Question Select multiple rows from DataGridView and save them to DataTable

    Hello, I have a DataGridView that contains several rows. Say, I select 5 rows from it, click a "Save" button, but only the 5 rows I selected are going to be saved to the DataTable.

    I already know how to activate the property to multiselect the rows. The problem is I don't know know to save the selected rows to the DataTable (with only a selected row I have no issue).
    Is there an example at CodeBank? Or any idea on how to achieve this?

    Thank you.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Rocket city, U.S.A.
    Posts
    1,230

    Re: Select multiple rows from DataGridView and save them to DataTable

    Maybe something like this (unless, of course, I'm misunderstanding the question):
    vb Code:
    1. For Each row As DataGridViewRow In Me.DataGridView1.Rows
    2.             If row.Selected Then
    3.                 'add a row to the datatable
    4.             End If
    5.         Next

    But now, how exactly is the data getting into the DataGridView in the first place?
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Re: Select multiple rows from DataGridView and save them to DataTable

    Quote Originally Posted by dolot View Post
    Maybe something like this (unless, of course, I'm misunderstanding the question):
    vb Code:
    1. For Each row As DataGridViewRow In Me.DataGridView1.Rows
    2.             If row.Selected Then
    3.                 'add a row to the datatable
    4.             End If
    5.         Next

    But now, how exactly is the data getting into the DataGridView in the first place?
    Thank you, I'll give it a try.

    PS: The data is getting into the DataGridView from a SQL table called "Menu" (the kind of menu you have at the restaurant). So, if a client orders, they are not going to order only one meal or only one drink; they are going to order multiple drinks, meals, desserts, appetizers,... at once (specially if it's not only a client, but a romantic couple or two or more friends sharing a table). And the restaurant manager or employee is not going to like to add everything the client ordered, one by one; neither the clients are going to like having to wait extra time. Then, I'll add a new column to the DataGridView: "Cantity"; if the row is selected, but cantity is null, then it won't be saved.

    The selected rows and cantity will be stored on the DataTable, which it turn will be saved to a "Order" table.
    Last edited by soulblazer; Jan 6th, 2015 at 01:01 PM.

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.