数据网格视图单元格内容单击错误 [英] Data Grid view cell content click error
问题描述
我创建了2个表单1.EmpPerInfo 2.Search,当用户点击EmpPerInfo表单的搜索按钮时,将打开搜索表单。然后用户将放置EmpNo。 &安培;点击查找按钮。然后匹配的行/记录将显示在Gridview中。
上述步骤的代码工作正常。但现在我想要用户点击名称数据网格视图系统的列将显示EmpPerInfo形式的记录,我已经写了下面提到的代码。
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.
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
但是EmpPerInfo.txtSrn.Text = dt.Rows(EmpNo).ItemArray(0).ToString()在此语句系统中引发错误位置0没有行
。
but "EmpPerInfo.txtSrn.Text = dt.Rows(EmpNo).ItemArray(0).ToString()" at this statement system throws an error "There is no row at position 0"
.
推荐答案
首先,搜索算法效率低下。而不是循环遍历行集合,使用SELECT方法过滤数据 [ ^ ]。
这是另一种方法: DataGridView Filter [< a href =http://code.msdn.microsoft.com/DataGridView-Filter-db565575target =_ blanktitle =New Window> ^ ]
其次,几种方法在表单之间传递数据:
在应用程序中的对象之间传递数据 [ ^ ]
演练:在Windows窗体之间传递数据 [ ^ ]
在Windows窗体之间传递值 [ ^ ]
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[^]
数据网格视图中没有位置0
当用户点击列名称,它被视为单击事件。由于没有点击一行,行被设置为0.
试试这个
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
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
这篇关于数据网格视图单元格内容单击错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!