Cast a DatagridView row to a DataRow

1,610 views
Skip to first unread message

Jeff Steinkamp (N7YG)

unread,
Oct 11, 2010, 11:51:04 AM10/11/10
to dotnetde...@googlegroups.com
I have a DatagridView that is populated from a SQL statement.  The user has the option of filtering that DataGridView even further.  What I need to be able to do is either cast the filtered results back to a DataTable, or iterate over the filtered results and cast each row back to a DataRow so I can feed that to a specialized  export routine.
 
Any idea how to accomplish this?
 
Jeff K. Steinkamp N7YG
Tucson, AZ
SCUD Missile Coordinates:
N032-13-55.02  W110-55-52.79
Registered Linux User: 420428
------------------------------------------------------
 
EMPLOYERS do it to employees

Stephen Russell

unread,
Oct 12, 2010, 8:31:22 AM10/12/10
to dotnetde...@googlegroups.com
On Mon, Oct 11, 2010 at 10:51 AM, Jeff Steinkamp (N7YG) <n7...@n7yg.com> wrote:
> I have a DatagridView that is populated from a SQL statement.  The user has
> the option of filtering that DataGridView even further.  What I need to be
> able to do is either cast the filtered results back to a DataTable, or
> iterate over the filtered results and cast each row back to a DataRow so I
> can feed that to a specialized  export routine.
>
> Any idea how to accomplish this?
-----------------------

You were so close.

DataView :

<http://davidhayden.com/blog/dave/archive/2006/02/11/2798.aspx>


DataView to DataTable:
<http://www.daniweb.com/code/snippet217027.html>

--
Stephen Russell

Sr. Production Systems Programmer
CIMSgts

901.246-0159 cell

Jeff Steinkamp (N7YG)

unread,
Oct 12, 2010, 7:12:11 PM10/12/10
to dotnetde...@googlegroups.com
?Actually, this was a lot easier to do than I thought. I have a Datasource
(ds) that is tied to a BindingSource and the DataGrivdView so I just did the
following

For Each row As DataRow In ds.Tables(0).Select(userFilter)

'do the specialized processing on the row
Next

Now that I have proved this works, I can eliminate about 50 lines of code
with another routine where I am processing each cell in the grid by pulling
a compete row and do the processing in a couple lines of code.


Jeff K. Steinkamp N7YG
Tucson, AZ
SCUD Missile Coordinates:
N032-13-55.02 W110-55-52.79
Registered Linux User: 420428
------------------------------------------------------

Whoever profits by the crime is guilty of it.


--------------------------------------------------
From: "Stephen Russell" <sruss...@gmail.com>
Sent: Tuesday, October 12, 2010 5:31 AM
To: <dotnetde...@googlegroups.com>
Subject: Re: [DotNetDevelopment] Cast a DatagridView row to a DataRow

Stephen Russell

unread,
Oct 13, 2010, 9:12:53 AM10/13/10
to dotnetde...@googlegroups.com
On Tue, Oct 12, 2010 at 6:12 PM, Jeff Steinkamp (N7YG)
<jkste...@gmail.com> wrote:
> ?Actually, this was a lot easier to do than I thought.  I have a Datasource
> (ds) that is tied to a BindingSource and the DataGrivdView so I just did the
> following
>
>        For Each row As DataRow In ds.Tables(0).Select(userFilter)
>
>                'do the specialized processing on the row
>               Next
>
> Now that I have proved this works, I can eliminate about 50 lines of code
> with another routine where I am processing each cell in the grid by pulling
> a compete row and do the processing in a couple lines of code.
-----------------

Sounds good. Your changes are done to the ds.dt so they are ready to
be updated if need be as well.

Arsalan Tamiz

unread,
Oct 14, 2010, 2:35:10 AM10/14/10
to dotnetde...@googlegroups.com
BTW,

DataGridView's Row has a property DataBoundItem this property returns the DataRowView in case you have bind the DataGridView with a BindingSource. Therefore casting can be done,

Dim drv as DataRowView
Dim objDataRow as DataRow

drv = Me.DataGridView1.Rows(0).DataBoundItem, DataRowView
objDataRow = drv.Row
Reply all
Reply to author
Forward
0 new messages