Datagrid question

J

John Wright

I have a form that asked the user to look up and item. If the lookup
produces more than one result, I pass the resulting datatable to another
form I call ResultForm and display the results for the user. The user can
look at the data and select the item they want, then I want to take the
selected row, put it into a datatable and return it to the calling form.
From the button click I call the search function and return the results. If
there datatable contains more than one row, I call:

My.Forms.SearchResult.LoadGrid(dtCrushing, tpCrushing)

The first argument is the datatable with the results, the second is the tab
that was displayed when the search was made. I load the grid and display
the results. In the results form I want to do the following:

Dim dtTempData As New DataTable

Dim tempRow As DataGridViewRow

tempRow = dgResults.CurrentRow

'load the data on the tab page by calling the appropriate load data function

Select Case tpCaller.Name

Case "tpCrushing"

My.Forms.Form1.LoadCrushingData(dtTempData)

End Select

Depending on the result, I want to load the selected item from the grid into
a datatable and push it back to the tabpage and call the public function to
load the data. How can I take the CurrentRow and load it into a datatable
to pass back to the tab page. The LoadCrushingData function called from
this form is also used on the tab page to load the data if only one row is
returned. Any help is appreciated.


Thanks.

John



CODE:



Calling form code:
Private Sub cmdFindCrushingRun_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdFindCrushingRun.Click

Try

If txtCrushingRunNumber.Text.Length > 0 Then

'call the function to find the run number

Dim dtCrushing As New DataTable

Dim dlgRes As DialogResult

dtCrushing = FindCrushingRun(txtCrushingRunNumber.Text)

If IsNothing(dtCrushing) OrElse dtCrushing.Rows.Count <= 0 Then

dlgRes = MessageBox.Show("No run found, do you want to create a new run?",
"No Run Found", MessageBoxButtons.YesNo, MessageBoxIcon.Information)

If dlgRes = Windows.Forms.DialogResult.Yes Then

'check the run number, if the first digit is not an alpha then prompt for
the correct

'format for the run number

Dim boolRunValid As Boolean = ValidateRunNumber(txtCrushingRunNumber.Text)

If boolRunValid = False Then

MessageBox.Show("The run number must start with a character. Please fix the
run number and try again.", "Invalid Run Number", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)

txtCrushingRunNumber.Focus()

Exit Sub

End If

Dim runID As Integer

runID = CreateNewCrushingRun(txtCrushingRunNumber.Text)

'check the run ID, it should be >0 if not there was an error.

If runID > 0 Then

'set the changes flag

boolChanges = True

currentRunID = runID

tsRunID.Text = "Run ID: " & currentRunID.ToString

Label10.Visible = True

Label10.Text = "Active"

Else

currentRunID = 0

End If

End If

Else

'Check the row count, if more than 1 then display a popup to show all the
options

'and have them select the run

If dtCrushing.Rows.Count > 1 Then

'show the search result form

My.Forms.SearchResult.LoadGrid(dtCrushing, tpCrushing)

My.Forms.SearchResult.Show()

Exit Sub

End If

LoadCrushingData(dtCrushing)

End If

End If

Catch ex As Exception

End Try

End Sub



Search Result Form code:



Private Sub btnSelectItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSelectItem.Click

'create the temp datatable from the selected item in the grid

Dim dtTempData As New DataTable

Dim tempRow As DataGridViewRow

tempRow = dgResults.CurrentRow

'load the data on the tab page by calling the appropriate load data function

Select Case tpCaller.Name

Case "tpCrushing"

My.Forms.Form1.LoadCrushingData(dtTempData)

End Select

End Sub
 
C

Cor Ligthert[MVP]

John,

Are you sure that you are using a Datagrid, you are talking in your text
about a DataGridViewRow, you cannot use the latter in a DataGrid.

However the answer would be the same for both, never access direct a complex
datacontrol, always do that using the underlying datasource.

In your case I would be more precise with my question. In this case it is
only puzling, or in other words making short answers, because it most
probably don't fit.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top