Connecting Tech Pros Worldwide Help | Site Map

retrieve rows from datagridview into a separate datatable?

Rich
Guest
 
Posts: n/a
#1: May 17 '06
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
Guest
 
Posts: n/a
#2: May 17 '06

re: retrieve rows from datagridview into a separate datatable?


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).Va lue.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:
[color=blue]
> 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
>
>[/color]
+Vice
Guest
 
Posts: n/a
#3: May 18 '06

re: retrieve rows from datagridview into a separate datatable?


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.

"Rich" <Rich@discussions.microsoft.com> wrote in message
news:285A945D-F37D-436F-8B6A-981BD4D51FA6@microsoft.com...[color=blue]
> 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).Va lue.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:
>[color=green]
>> 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
>>
>>[/color][/color]


Cor Ligthert [MVP]
Guest
 
Posts: n/a
#4: May 18 '06

re: retrieve rows from datagridview into a separate datatable?


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?...7-d6ad9bebf57f

I hope this helps,

Cor

"Rich" <Rich@discussions.microsoft.com> schreef in bericht
news:3093696F-5CD5-4172-88D0-E94E86AF9B65@microsoft.com...[color=blue]
> 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
>
>[/color]


Closed Thread