如何在VB.NET中处理datagridview中的索引问题? [英] How do I deal with the index problem in datagridview in VB.NET?

查看:90
本文介绍了如何在VB.NET中处理datagridview中的索引问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Datagridview只有两行。我设置了allowusertoaddnewrow = false。我在数据库中填充datagridview中的行。我想通过按Enter键在KeyDown事件的文本框中传输其值。当我在datagridview中按Enter键选择第一行的内容时,返回datagridview.currentrow.index 1.以同样的方式当我选择第二行并按Enter键时,同样发生在这里,意味着它也返回datagridview .currentrow.index = 1.有没有任何概念可以解决这个问题。



我尝试了什么:



RowIndx = Me.DataGridView1.CurrentRow.Index

My Datagridview has only two rows. I have set allowusertoaddnewrow = false. I am filling rows in datagridview from database. I want to transfer its value in a textbox on KeyDown Event by pressing Enter Key. When I Press Enter key in datagridview to select the contents of first row, the datagridview.currentrow.index is returned 1. In the same way when I select the second row and press Enter Key then same happens here, means it also returns the datagridview.currentrow.index = 1. Is there any concept so that I can deal with this problem.

What I have tried:

RowIndx = Me.DataGridView1.CurrentRow.Index

推荐答案

尝试 DataGridView.SelectedRows (0)而不是。


感谢您的回复。



我也试过了,但是这对我来说无济于事。



我通过代码获得解决方案:





Private Sub DataGridView1_EditingControlShowing(sender As System.Object,e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)处理DataGridView1.EditingControlShowing



Dim DataG ridView1 As DataGridView = CType(e.Control,DataGridView)

AddHandler DataGridView1.PreviewKeyDown,AddressOf DataGridView1_PreviewKeyDown



End Sub



Private Sub DataGridView1_PreviewKeyDown(发送者作为对象,e作为PreviewKeyDownEventArgs)处理DataGridView1.PreviewKeyDown

Dim TBox作为TextBox

Dim RowNum As整数

尝试

如果e.KeyCode = Keys.Enter或e.KeyCode = Keys.Return然后

TBox = Me.Owner.ActiveControl

RowNum = Me.DataGridView1.CurrentRow.Index

调用DGridToText(TBox,RowNum)

结束如果

C ex ex As Exception

Err.Description

结束尝试

End Sub
Thanks for reply.

I had tried that also, but this couldn't helped me.

I got solution by using code :


Private Sub DataGridView1_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

Dim DataGridView1 As DataGridView = CType(e.Control, DataGridView)
AddHandler DataGridView1.PreviewKeyDown, AddressOf DataGridView1_PreviewKeyDown

End Sub

Private Sub DataGridView1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles DataGridView1.PreviewKeyDown
Dim TBox As TextBox
Dim RowNum As Integer
Try
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
TBox = Me.Owner.ActiveControl
RowNum = Me.DataGridView1.CurrentRow.Index
Call DGridToText(TBox, RowNum)
End If
Catch ex As Exception
Err.Description
End Try
End Sub


这篇关于如何在VB.NET中处理datagridview中的索引问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆