Cesar Francisco

Code Snippet

Option Strict On

Public Class HClothingSeeAll

Private Sub HClothingSeeAll_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'TODO: This line of code loads data into the 'HOEXDBDataSet.Table2' table. You can move, or remove it, as needed.

Me.Table2TableAdapter.FillByClothing(Me.HOEXDBDataSet.Table2)

End Sub

Dim entryform As New HClothingForm

Private Sub Table2DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table2DataGridView.CellMouseDoubleClick

entryform.TextBox9.Text = (Me.Table2DataGridView.CurrentRow.DataBoundItem("ID"))

entryform.Show()

entryform.Table2TableAdapter.FillByCloth(entryform.HOEXDBDataSet.Table2, Integer.Parse(entryform.TextBox9.Text))

Me.Close()

End Sub

End Class

Option Strict On disallows late binding.

What is the remedy form this problem


Re: Visual Basic General Option Strict On and late binding

APMadsen

When Option Strict is on - which it always should be) you must supply type information at all times. Boring perhaps, but it makes your code more secure, and it runs faster to.

Supply type information, is needed whenever you use the type object. If you set TextBox9.Text = Something As Object, a runtime error could occur if Something was an image or something else other than a string.

Solution:

Use CType to supply the type information...

Code Snippet

If TypeOf(Table2DataGridView.CurrentRow.DataBoundItem("ID")) Is String _

Or IsNumeric(Table2DataGridView.CurrentRow.DataBoundItem("ID")) Then

TextBox9.Text = Ctype(Table2DataGridView.CurrentRow.DataBoundItem("ID"), String)End If






Re: Visual Basic General Option Strict On and late binding

Cesar Francisco

Brilliant.Thanks.

While the If then ... did not work, your general suggestion did for this part of the code.

Code Snippet

Private Sub Table2DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table2DataGridView.CellMouseDoubleClick

entryform.Table2TableAdapter.FillByCloth(entryform.HOEXDBDataSet.Table2, CType(entryform.TextBox9.Text, Integer))

Me.Close()

End Sub

On the other hand, this part do not......

Code Snippet

Private Sub Table2DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table2DataGridView.CellMouseDoubleClick

entryform.Show()

entryform.TextBox9.Text = CType(Table2DataGridView.CurrentRow.DataBoundItem("ID"), String)

entryform.Table2TableAdapter.FillByCloth(entryform.HOEXDBDataSet.Table2, CType(entryform.TextBox9.Text, Integer))

entryform.ToolStripStatusLabel1.Text = " Record is ready to modify"

HOEX.HClothingForm.Close()

Me.Close()

End Sub

This version -->

A = CType(Table2DataGridView.CurrentCell.Value, Integer).

would work also, but to get the row's primary key value (ID) the 'click' has to be made on the "ID" column only.

The problem is at --> ("ID")






Re: Visual Basic General Option Strict On and late binding

GreatDane

Well the CType function only works if the contents of the data-field indeed can be converted to a string - therefore you must trap these errors before doing this:

Code Snippet

'Checks if field is empty - you can't use keyword Nothing here, because

'it doesn't apply to databases...

If Not Table2DataGridView.CurrentRow.DataBoundItem("ID") Is System.DBNull.Value Then

entryform.TextBox9.Text = CType(Table2DataGridView.CurrentRow.DataBoundItem("ID"), String)

End If






Re: Visual Basic General Option Strict On and late binding

Cesar Francisco

Nope. The problem persist for the same reason. Take the "ID" out, and all is well.

Squiggly lines under Table1DataGridView.CurrentRow.DataBoundItem("ID")

Or let Option Strict On fall which solve the problem either way.






Re: Visual Basic General Option Strict On and late binding

GreatDane

We must track the problem back to the root - the Table2DataGridView.CurrentRow.DataBoundItem...

Since the Table2DataGridView.CurrentRow.DataBoundItem can be of any type (typed as object), and/or nothing, you must test for that as well:

Code Snippet

If Not Table2DataGridView.CurrentRow.DataBoundItem Is Nothing Then

If Not Table2DataGridView.CurrentRow.DataBoundItem("ID") Is Nothing Then

If Not Table2DataGridView.CurrentRow.DataBoundItem("ID") Is System.DBNull.Value Then

entryform.TextBox9.Text = Cstr(Table2DataGridView.CurrentRow.DataBoundItem("ID"))

End If

End If

End If

If you hold the mouse over the lines highlighted when debugging, a popup will show you if that particular object has been assigned a value.





Re: Visual Basic General Option Strict On and late binding

Cesar Francisco

Appreciated. Nevertheless the problem continue to persist.

This is an extremely small database (Access type) ID (which is the key - has an integer value, always. It is never nothing)

The data row is: ID, First Name, Last Name, DOB

Code Snippet

Private Sub Table1DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table1DataGridView.CellMouseDoubleClick

Me.TabControl1.SelectedTab = TabPage1

If Not Table1DataGridView.CurrentRow.DataBoundItem Is Nothing Then

If Not Table1DataGridView.CurrentRow.DataBoundItem("ID") Is Nothing Then

If Not Table1DataGridView.CurrentRow.DataBoundItem("ID") Is System.DBNull.Value Then

txtboxID.Text = CStr(Table1DataGridView.CurrentRow.DataBoundItem("ID"))

End If

End If

End If

End Sub

Error : 1,2,3 Option Stric On disallows late binding.




Re: Visual Basic General Option Strict On and late binding

Cesar Francisco

Thank you AbooJCH !

In the TabPages scenario your code is perfect.

DataGridView >Behavior>AllowUserToResizeRows has to be set to False

Code Snippet

Private Sub Table1DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table1DataGridView.CellMouseDoubleClick

If Table1DataGridView.CurrentRow.DataBoundItem Is Nothing Then

Me.Table1TableAdapter.Fill(Me.NewAppDBDataSet.Table1)

'Clear TextBoxes

grpbClear()

tbctp1Clear()

ElseIf e.RowIndex >= 0 Then

Me.TabControl1.SelectedTab = TabPage1

txtboxID.Text = Table1DataGridView.Rows(e.RowIndex).Cells("ID").Value.ToString

Table1TableAdapter.FillByID(NewAppDBDataSet.Table1, CType(txtboxID.Text, Integer))

End If

End Sub

Modifying a little bit your idea, this code below works in the case of two separate Forms (passing data from Form to an other Form).

Code Snippet

Option Strict On

Public Class HClothingSeeAll (Form2 containing the DataGridView)

'HClothingForm is Form1 Data Entry Form, entryform is an instance of it

Public entryform As New HClothingForm )

Private Sub HClothingSeeAll_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Fill Form2's DataGridView with data

Me.Table2TableAdapter.FillByClothing(Me.HOEXDBDataSet.Table2)

End Sub

Private Sub Table2DataGridView_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table2DataGridView.RowHeaderMouseDoubleClick

entryform.Show()

'This line is to handle the last (or new) empty row.

If Table2DataGridView.CurrentRow.IsNewRow = True Then

Me.Close()

Exit Sub

'This line is liminting the column selection to the first column (column 0)

ElseIf e.ColumnIndex < 1 Then

'Getting the ID (Key value)

entryform.TextBox9.Text = Table2DataGridView.Rows(e.RowIndex).Cells(0).Value.ToString

'Filling the Data Entry Form with data using the Key from the Form containing the DataGridView

entryform.Table2TableAdapter.FillByCloth(entryform.HOEXDBDataSet.Table2, CType(entryform.TextBox9.Text, Integer))

entryform.ToolStripStatusLabel1.Text = " Record is ready to modify"

'Close original Form (instead of its instance)

HOEX.HClothingForm.Close()

'Close DataGridView

Me.Close()

End If

End Sub

End Class
The problem remaining to solve:
How to get the Key from a selected row (not from Rowheader)
Solution:

Code Snippet

Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick

If DataGridView1.CurrentRow.IsNewRow = True Or e.RowIndex = -1 Then

Exit Sub

Else

entryform2.Show()

entryform2.TextBox9.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString

MsgBox(entryform2.TextBox9.Text)

entryform2.Table2TableAdapter.FillByFoo(entryform2.HOEXDBDataSet.Table2, Integer.Parse(entryform2.TextBox9.Text))

entryform2.ToolStripStatusLabel1.Text = " Record is ready to modify"

HOEX.HFoodForm.Close()

Me.Close()

End If

End Sub