Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to Fill a dataset from datagrid

Posted on 2005-05-16
Medium Priority
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
  • 2
  • 2
LVL 28

Expert Comment

ID: 14009397
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

ID: 14009862
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

iboutchkine earned 500 total points
ID: 14009960
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

ID: 14010350
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. :)


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

571 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