473,327 Members | 2,118 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,327 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 32220
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.