Dear Sir,
I'm trying to add value from datagridview search form (frmSearch) to datagridview main form (frmMain) from datagridview search form (frmSearch) but
I have an error `Conversion from string "" to type 'Integer' is not valid.`
Please Gude me
Imports System.ComponentModel
Public Class FrmSearch
Public _rowIndexMainGrid As String = ""
Public _LongName As String = ""
Public _Surname As String = ""
Public Property RowIndexMainGrid As String
Get
Return _rowIndexMainGrid
End Get
Set(ByVal value As String)
_rowIndexMainGrid = value
End Set
End Property
Public Property LongName As String
Get
Return _LongName
End Get
Set(ByVal value As String)
_LongName = value
End Set
End Property
Public Property Surname As String
Get
Return _Surname
End Get
Set(ByVal value As String)
_Surname = value
End Set
End Property
Sub BindGrid(ByVal isSearch As Boolean, ByVal searchBy As String)
Dim persons As New List(Of Person) From {
New Person With {
.LongName = "Joe",
.Surname = "Black"
},
New Person With {
.LongName = "Misha",
.Surname = "Kozlov"
}
}
Dim bindingList As New BindingList(Of Person)(persons)
Dim source = New BindingSource(bindingList, Nothing)
DataGridView1.DataSource = source
DataGridView1.Refresh()
DataGridView1.ColumnHeadersVisible = True
DataGridView1.Columns(0).Width = 100
DataGridView1.Columns(1).Width = 350
DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.ScrollBars = ScrollBars.Both
DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
DataGridView1.AllowUserToAddRows = False
DataGridView1.ReadOnly = True
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub FrmSearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindGrid(False, "")
End Sub
Private Sub DataGridView1_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
SelectedRow(e.RowIndex)
End Sub
Sub SelectedRow(ByVal index As Integer)
Me.Hide()
Dim rowIndexMainGrid = CInt(txtRowIndex.Text)
_rowIndexMainGrid = rowIndexMainGrid
_LongName = DataGridView1.Rows(index).Cells(0).Value
_Surname = DataGridView1.Rows(index).Cells(1).Value
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Hide()
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
SelectedRow(CInt(txtRowIndexSearch.Text))
End Sub
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
txtRowIndexSearch.Text = e.RowIndex
End Sub
End Class
Code in frmMain
Imports System.ComponentModel
Public Class frmMain
Private persons As New List(Of Person)
Public flag As Boolean = False
Sub BindGrid()
Dim bindingList As New BindingList(Of Person)(persons)
Dim source = New BindingSource(bindingList, Nothing)
DataGridView1.DataSource = source
source.AddNew()
DataGridView1.ColumnHeadersVisible = True
Dim btn As New DataGridViewButtonColumn
btn.HeaderText = ""
btn.Text = "SEARCH"
btn.Name = "btn"
btn.DefaultCellStyle.BackColor = Color.Black
btn.DefaultCellStyle.ForeColor = Color.White
btn.UseColumnTextForButtonValue = True
DataGridView1.Columns.Insert(0, btn)
DataGridView1.Columns(0).Width = 50
DataGridView1.Columns(1).Width = 100
DataGridView1.Columns(2).Width = 100
DataGridView1.Columns(0).ReadOnly = True
DataGridView1.Columns(1).ReadOnly = False
DataGridView1.Columns(2).ReadOnly = True
DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.AllowUserToAddRows = False
DataGridView1.ScrollBars = ScrollBars.Both
DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtRowIdx.Text = "0"
BindGrid()
End Sub
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
txtRowIdx.Text = e.RowIndex
If e.Button = MouseButtons.Left AndAlso e.ColumnIndex = 0 Then
Dim frmSearch As FrmSearch = New FrmSearch()
'frmSearch.txtRowIndex.Text = e.RowIndex
'frmSearch.txtRowIndexSearch.Text = "0"
Dim result As DialogResult = frmSearch.ShowDialog()
If result = DialogResult.OK Then
DataGridView1.Rows(frmSearch.RowIndexMainGrid).Cells(1).Value = frmSearch.LongName
DataGridView1.Rows(frmSearch.RowIndexMainGrid).Cells(2).Value = frmSearch.Surname
End If
End If
If e.ColumnIndex = 1 Then
Try
If flag = True Then
If Not IsDBNull(DataGridView1.Rows(txtRowIdx.Text).Cells(1).Value) Then
DataGridView1.CurrentCell = DataGridView1.Rows(txtRowIdx.Text).Cells(1)
End If
End If
Catch ex As Exception
Return
End Try
End If
End Sub
End Class
Public Class Person
Public Property LongName As String
Public Property Surname As String
End Class
Thanks