By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,069 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,069 IT Pros & Developers. It's quick & easy.

retrieve rows from datagridview into a separate datatable?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.