Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert DataRow to DataGridViewRow or DataGridViewRow to DataRow

Status
Not open for further replies.

ousoonerjoe

Programmer
Jun 12, 2007
925
US
Does anyone have a good way to covert a DataRow and a DataGridViewRow back and forth? Depending on the direction of data, is the direction needed and I'm just getting irritated with the bulky IF statements and conversions.

Problem is I have a grid that may or may not have a DataSource loaded at the time of data entry. Various options will insert a row to the grid with predefined values. If the DataSource is loaded, then a DataRow is needed to import to the DataTable. If not, then a DataGridViewRow is needed to add to the DataGridView.

Any suggestions are welcome.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 

I would find a way to always have a DataSource, and always use a DataRow. Not knowing exactly what you are doing I can't give any specific (code) suggestions regarding how to implement this, but in the situation of no DataSource at the time of data entry (I'm assuming the grid is empty), use an empty DataSource and add rows to it as needed. It seems this would be much more efficient and easier to maintain, rather than the If statements and conversions.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Vb.Net 2010.

Ok. Here's one instance. It's an accounting process. The user needs to "split" a row. I want to take the selected row, make x number of copies, change a couple fields in all rows involved (original and new) and insert the new rows in. This function will always occur when the grid is bound to a DataTable. All saves to the database will occur when the user has completed processing the item and is ready for the next one. We have found that giving them the ability to just close and start over was a LOT easier on us, and allows for an "All or Nothing" approach that they are used to from the old

In this scenario, what would be the most efficient approach? I have found that when passing .Clone to a variable, the original row is updated as well. Very irritating. Implying that .Clone should have a ByRef/ByVal option to it. I have it working, but there has to be a better way than the cluster I have created to pull it off.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Here is a code sample of where we need to "move accounts" (This is a lot cleaner than originally written):

1) Row is selected.
2) Row is copied (DrView).
3) Row is changed to a Negative value.
4) Negative Value Row is added to grid.
5) Return Row to original positive value and clear the Account Number.
6) Add row to grid.
7) Return to user with focus at last inserted row.

Original Selected Row should never be changed.
Only 2 rows should be added.

The problem with the following code; the original row changes as the above changes are made. How do I get DrView to be a COPY of the original rather than a referance to the original row?
Code:
		Dim DrView As DataRowView = Nothing
		Dim DgrDt As DataTable = Nothing

			DgrDt = dgDetail.DataSource
			DrView = DirectCast(dgDetail.SelectedRows(0).DataBoundItem, DataRowView)
			DrView.Item("Amount") = dgDetail.Item(dcAmount.Index, dgDetail.SelectedRows(0).Index).Value * -1
			DrView.Row("PaidAmt") = dgDetail.Item(dcPaidAmt.Index, dgDetail.SelectedRows(0).Index).Value * -1
			DrView.Row("DueDate") = Today.ToString("MM/dd/yyyy")
			DrView.Row("DetailNotes") = Comments
			DrView.Row("InvNum") = InvNum
			DgrDt.ImportRow(DrView.Row)
			DrView.Row("Amount") = DrView.Row("Amount") * -1
			DrView.Row("PaidAmt") = DrView.Row("PaidAmt") * -1
			DrView.Row("Account") = ""
			DgrDt.ImportRow(DrView.Row)
			dgDetail.CurrentCell = dgDetail.Item(dcPayIt.Index, ApHeadId)
			dgDetail.Refresh()

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top