Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have created 2 forms 1.EmpPerInfo 2.Search, When user click on "Search" button of EmpPerInfo form then Search form will be open. Then user will put EmpNo. & click on find button.Then matched row/record will be displayed in Gridview.

Code for above mentioned steps are working fine.but Now I want when user click on Name column of Data Grid View system will show that record in "EmpPerInfo" form for this i have written below mentioned code.

VB
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles EmployeeGrid.CellContentClick
       If (e.ColumnIndex <> -1) Then
           If (e.RowIndex > -1) Then
               EmpNo = e.RowIndex

               EmpPerInfo.txtSrn.Text = dt.Rows(EmpNo).ItemArray(0).ToString()
               EmpPerInfo.txtFullName.Text = dt.Rows(EmpNo).ItemArray(2).ToString()
               EmpPerInfo.Show()

               
           End If
       End If
   End Sub


but "EmpPerInfo.txtSrn.Text = dt.Rows(EmpNo).ItemArray(0).ToString()" at this statement system throws an error "There is no row at position 0"

.
Posted
Updated 22-Dec-13 22:29pm
v2
Comments
jackspero18 23-Dec-13 2:39am    
ok now when you click on row then you want the info on the EmpperInfo of the selected row..??
Yogi ,Pune 23-Dec-13 2:45am    
Yes

First of all, searching algorithm is inefficient. Rather then looping through the collection of rows, filter data using SELECT method[^].
Here is another method: DataGridView Filter[^]

Second of all, there are few ways to pass data between forms:
Passing Data Between Objects in an Application[^]
Walkthrough: Passing Data Between Windows Forms[^]
Pass values between windows forms[^]
 
Share this answer
 
There is no position 0 in data grid view.
When the user clicks on the column name, It counts as a click event. Since A row was not clicked the row is set to 0.

Try this



VB
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles EmployeeGrid.CellContentClick
       
           If (e.RowIndex > 0) Then
               EmpNo = e.RowIndex
 
               EmpPerInfo.txtSrn.Text = dt.Rows(EmpNo).ItemArray(0).ToString()
               EmpPerInfo.txtFullName.Text = dt.Rows(EmpNo).ItemArray(2).ToString()
               EmpPerInfo.Show()
 
               
           End If
      
   End Sub
 
Share this answer
 
Comments
Yogi ,Pune 23-Dec-13 23:54pm    
I have tried your code " If (e.RowIndex > 0) Then"
when I debug this code, i found that RowIndex=0. i.e in code we checked 0 > 0 hence condition is not fulfill, so system is not executing any statement

then I tried (e.RowIndex >= 0) then again same error occurred.There is no row at position 0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900