|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
retrieve rows from datagridview into a separate datatable?Hello,
I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable filtered by the dataview into a separate table? I only want to copy the rows from the main table that are filtered by the dataview. Is there a way to do this without having to loop through the entire main datatable? How to do this? Thanks, Rich Well, here are my findings (for posterity and incase anyone cares).
'--eliminate a row for starting at 0 '--the last row in the datagridview is an insert row - not a real row yet For i = 0 To datagridview.Rows.Count - 2 For j = 0 To datagridview.Columns.Count - 1 Console.WriteLine(datagridview.Rows(i).Cells(j).Value.ToString) Next Next --to add rows to a datatable from the datagrid I had to loop through all the cells in each row of the datagrid. Dim dt As DataTable, dr As DataRow dt = dataset.Tables("mainTable").Copy dt.Rows.Clear For i = 0 To datagridview.Rows.Count - 2 dr = dt.NewRow For j = 0 To datagridview.Columns.Count - 1 dr(j) = datagridview.Rows(i).Cells(j).Value. Next dt.Rows.Add(dr) Next The alternative would be to loop through the entire main datatable and copy only the rows that I want, but I don't have to loop through the columns. I can just copy the row(s) and add it to the separate datatable. Ideally, I would like to copy only the rows being displayed in the datagridview (which is filtered by the dataview) without having to loop through the cells in the datagridview. If this is doable - could someone please share? I am trying to get away from klunky/kludgy coding. Thanks, Rich Show quoteHide quote "Rich" wrote: > Hello, > > I am populating a datagridview from a datatable and filtering the number of > rows with a dataview object. Is there a way to retrieve the rows displayed > by the datagridview into a separate datatable without having to loop through > each column in the datagridview? Or is there a way to retrieve the rows from > the original datatable filtered by the dataview into a separate table? I > only want to copy the rows from the main table that are filtered by the > dataview. Is there a way to do this without having to loop through the > entire main datatable? How to do this? > > Thanks, > Rich > > The dataview should have it's own rows like the datatable. Why don't you
try and add the dataview's row into the datatable instead. I've never really done this but I'm sure there are easier ways then what you have. Show quoteHide quote "Rich" <R***@discussions.microsoft.com> wrote in message news:285A945D-F37D-436F-8B6A-981BD4D51FA6@microsoft.com... > Well, here are my findings (for posterity and incase anyone cares). > > '--eliminate a row for starting at 0 > '--the last row in the datagridview is an insert row - not a real row yet > For i = 0 To datagridview.Rows.Count - 2 > For j = 0 To datagridview.Columns.Count - 1 > Console.WriteLine(datagridview.Rows(i).Cells(j).Value.ToString) > Next > Next > > --to add rows to a datatable from the datagrid I had to loop through all > the > cells in each row of the datagrid. > > Dim dt As DataTable, dr As DataRow > dt = dataset.Tables("mainTable").Copy > dt.Rows.Clear > For i = 0 To datagridview.Rows.Count - 2 > dr = dt.NewRow > For j = 0 To datagridview.Columns.Count - 1 > dr(j) = datagridview.Rows(i).Cells(j).Value. > Next > dt.Rows.Add(dr) > Next > > The alternative would be to loop through the entire main datatable and > copy > only the rows that I want, but I don't have to loop through the columns. > I > can just copy the row(s) and add it to the separate datatable. > > Ideally, I would like to copy only the rows being displayed in the > datagridview (which is filtered by the dataview) without having to loop > through the cells in the datagridview. If this is doable - could someone > please share? I am trying to get away from klunky/kludgy coding. > > Thanks, > Rich > "Rich" wrote: > >> Hello, >> >> I am populating a datagridview from a datatable and filtering the number >> of >> rows with a dataview object. Is there a way to retrieve the rows >> displayed >> by the datagridview into a separate datatable without having to loop >> through >> each column in the datagridview? Or is there a way to retrieve the rows >> from >> the original datatable filtered by the dataview into a separate table? I >> only want to copy the rows from the main table that are filtered by the >> dataview. Is there a way to do this without having to loop through the >> entire main datatable? How to do this? >> >> Thanks, >> Rich >> >> Rich,
In version 2.0 is the (much overloaded) method ToTable for that. http://msdn2.microsoft.com/de-de/library/hw5ayeet.aspx Otherwise you have to loop (You dont even need the sort) http://www.vb-tips.com/default.aspx?ID=655b418e-8e9d-4303-8be7-d6ad9bebf57f I hope this helps, Cor Show quoteHide quote "Rich" <R***@discussions.microsoft.com> schreef in bericht news:3093696F-5CD5-4172-88D0-E94E86AF9B65@microsoft.com... > Hello, > > I am populating a datagridview from a datatable and filtering the number > of > rows with a dataview object. Is there a way to retrieve the rows > displayed > by the datagridview into a separate datatable without having to loop > through > each column in the datagridview? Or is there a way to retrieve the rows > from > the original datatable filtered by the dataview into a separate table? I > only want to copy the rows from the main table that are filtered by the > dataview. Is there a way to do this without having to loop through the > entire main datatable? How to do this? > > Thanks, > Rich > >
Get Element Value
MAPI mail Listbox problem Maximum number of controls in a form Using Office functions question - type mismatch error how to go to specific row in hashtable based on value - not key? iDisposable and database connection to SQL How To Create New Form From Form Or Tabs On A Form how to add a reference in asp.net/vb.net? MDI |
|||||||||||||||||||||||