How to Fill a dataset from datagrid

Posted on 2005-05-16
Last Modified: 2010-04-23

In a Windows Application I have a datagrid that has some data displayed with databound from a dataview. The user is allowed to use some filters that are applied in the dataview. So far is easy. Now I want the user to be able to mark rows in the datagrid (using CTRL+click, or Shift+click) and when he press a button these marked rows be stored in a new dataset. How can I do that?
Thanks in advance.
Question by:eleos111
    LVL 28

    Expert Comment

    If hold down the ctrl key when clicking on rows in a datagrid you can
    select multiple rows.  You can not select multiple cells.

    'Determine selected rows

     Public Function GetSelectedRows(ByVal dg As DataGrid) As System.Collections.ArrayList
              Dim al As New ArrayList()
              Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember)
              Dim dv As DataView = CType(cm.List, DataView)
              Dim i As Integer
              For i = 0 to dv.Count - 1
                   If dg.IsSelected(i) Then
                   End If
              End Next
              Return al
         End Function 'GetSelectedRows
          Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
               Dim s As String = "Selected rows:"
              Dim o As Object
              For Each o In GetSelectedRows(dataGrid1)
                   s += " " + o.ToString()
              Next o
              MessageBox.Show(s) 'here you can addtot the datatable
          End Sub 'button1_Click

    Let me know if you need help adding to the datatable

    Author Comment

    Thanks for the reply.
    Of course can mark only rows. That is what I want to do.
    So, in 's' will be stored all the datagrid index for the selected rows. And there is an easy way to store the full data of that in a dataset? For example, does exist something like that? :

    for Each o in GetSelectedRows(datagrid1)
    next o

    Or should I read one by one the ids(that correspond to the dataset) from the datagrid using the above index and then find the corresponding data from the old dataset?
    LVL 28

    Accepted Solution

    I would rather do that

     For Each o In GetSelectedRows(dataGrid1)
              MyNewDataset.Tables(MyTableName).Add("FieldName", GetType(String))
            'add Value to field
            ds.Tables(0).Rows(0).Item("Value") = o.Item(0)
     Next o

    Author Comment

    Ok, thanks a lot for the help.

    Finally I used this one:    

            Dim dr2 As DataRow
            For Each o In GetSelectedRows(dgListEstate)

                dr2 = DsEstates2.ViewEstates.NewRow
                For i = 0 To dr2.ItemArray.Length - 1
                    dr2(i) = dvEstates.Item(o).Row.Item(i)
            Next o

    Instead of the loop there I was trying with that line:
     dr2 = dvEstates.Item(o.ToString()).Row
    but I was getting an error that row is already in use in another table. :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now