468,257 Members | 1,427 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

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
May 17 '06 #1
3 31702
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:
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

May 17 '06 #2
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" <Ri**@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.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).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:
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

May 18 '06 #3
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" <Ri**@discussions.microsoft.com> schreef in bericht
news:30**********************************@microsof t.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

May 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Luca Beretta | last post: by
4 posts views Thread by craig.wenger | last post: by
6 posts views Thread by =?Utf-8?B?TU1TSkVE?= | last post: by
3 posts views Thread by Christian | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.