473,221 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 32199
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Luca Beretta | last post by:
I think this a simple question but i'm breaking my brain without results. I've a datagridview bounded to a datatable, with an imagecolumn that should contain a different bitmap depending from a...
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
0
by: Dagan | last post by:
Hello I have 2 threads, one for adding\removing rows to\from DataTable that is bind to DataGridView and the main thread that views the DataGridView, when I change any row in the DataTable the...
1
by: lgalumbres | last post by:
Hi, I have a datagrid on Winform that is bound to a datatable. In essence, what I would like to do is that when a user inputs a number into a textbox control for example 5 then 5 empty rows...
4
by: craig.wenger | last post by:
I am using a DataGridView to display information stored in classes. I am linking to the information with the Tag property of the DataGridView rows. Unfortunately, whenever I set the Tag property,...
9
by: trint | last post by:
How can I retrieve all of the data from a datagridview? Any help is appreciated. Thanks, Trint
6
by: =?Utf-8?B?TU1TSkVE?= | last post by:
How to let user delete multi rows from the BindingSource while the SelectionMode Property set to RowHeaderSelect I have in my program datagridview bound it to sql table Throw Bindingsource To...
3
by: Christian | last post by:
Hi, i create aprogram that load a table from Access DB. i want color rows with different color but when i run the program and i load the datagridview the first time, the rows are white! only when...
3
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.