Saving Datagridview

thirteentwenty

Well-known member
Joined
Oct 9, 2008
Messages
80
Location
Honolulu
Programming Experience
Beginner
2 posts in one day... I'm starting to feel retarded... anyways here goes...

I've got a form that has a datagridview (DGV) (unbound) in it, I would like it to duplicate all the fields on it (done!) when I click the "Duplicate" button. All goes well sans the DGV... I can't even think of away to do it...

Thinks to be known about the table, it has autonumbers (I'm not sure if that's relevant) and it has one field that will need to be updated (transaction id, used for multiple line items... ).

I was thinking about using a loop or something like that but, I wasn't sure if that would be the proper thing to do.

any suggestions?
 
2 posts in one day... I'm starting to feel retarded... anyways here goes...

I've got a form that has a datagridview (DGV) (unbound) in it, I would like it to duplicate all the fields on it (done!) when I click the "Duplicate" button. All goes well sans the DGV... I can't even think of away to do it...

Thinks to be known about the table, it has autonumbers (I'm not sure if that's relevant) and it has one field that will need to be updated (transaction id, used for multiple line items... ).

I was thinking about using a loop or something like that but, I wasn't sure if that would be the proper thing to do.

any suggestions?

Your question isnt very clear. When you ask to duplicate all the fields in a DGV, youre asking to create a duplicate set of columns...

DGV dont have "fields" per se.. DGV dont even have any data at all. Think of them as a tool for showing you the data contained in a data model called a DataTable. This is why I say that DGV dont have fields; they have columns.
Further, DataTables dont have fields, they are a collection of DataRows. DataRows are the only things that could reasonably be asserted to have fields

Now, really get into understanding this because it helps you think and solve problems properly. Knowing that fields are contained in datarows, I think you want to:

Show rows to the user, and let the user pick one and press a [Duplicate] button

Every data entry on the row will be copied with the exception of the row id, which is an autonumber

Correct?

OK, well now we are thinking about things in the correct way, we know that our op is nothing to do with a DGV at all.

You use DirectCast(DirectCast(BindingSource.Current, DataRowView).Row, MyRowType) to retrieve the current row. Then you say something like:
VB.NET:
Dim ro as MyRowType = MyDataSet.MyDatatable.New[I]MyRowType[/I]Row
For Each dc as DataColumn in MyDataSet.MyDatatable
  If dc Is MyDataSet.MyDatatable.MyIDColumn Then Continue
  ro(dc) = currentRo(dc)
Next dc
MyDataSet.MyDatatable.AddMyRowTypeRow(ro)
 
Your question...

Thank you sir! I was looking at DGV all wrong... I knew it was just a viewer of sorts but didn't really know how to put it across... the only difference in what needs to be done and what you said is that everything in the DGV needs to be copied, not just what the user selects... something told me to ask before creating the loop (or in this case for each), as there might have been a better way to accomplish this.

Thank you again... I'd give you a hug but i think that you'd cut me before i got too close :D
 
That being the case, I'd probably be tempted to use:

DataTable.Copy 'to create a duplicate of the other datatable


Now, if Copy preserves AutoIncrement/ID number notions it may either:
a) increment the IDs leaving you with a copied table but new IDs
b) copy the IDs verbatim


If in the case of b) (dont know, never tried, you can have a play) I would simply loop over the whole datatable, adding N to every ID, where N is the maximum ID in the original..
..or whatever your ID scheme is, generate new IDs :)

FInally DataTable.Merge can be used to combine them, or you can work with them separately.

Note that RowStates may also be copied, so if you want to insert these rows to database, you may have to call SetAdded() on every single one (in the same loop as you refresh the IDs, perhaps)
 
...
b) copy the IDs verbatim


If in the case of b) (dont know, never tried, you can have a play) I would simply loop over the whole datatable, adding N to every ID, where N is the maximum ID in the original..
..or whatever your ID scheme is, generate new IDs :)

FInally DataTable.Merge can be used to combine them, or you can work with them separately.

Note that RowStates may also be copied, so if you want to insert these rows to database, you may have to call SetAdded() on every single one (in the same loop as you refresh the IDs, perhaps)

I ended up having to go with plan b.It did as predicted try to preserve the ID, so I made a For Each Next loop and added 1 to the ID and it wasn't near as painful as I originally thought it would be.

cjard you are a scholar and a gentleman.... thank you for your help!
 

Latest posts

Back
Top