DataGridView的SelectionChange事件多次触发。 [英] DataGridView's SelectionChange event fires many times.

查看:406
本文介绍了DataGridView的SelectionChange事件多次触发。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用datagridview时遇到了一些问题.Datagridview的selectionchange事件触发了几次,

由于这些触发器,函数编写的代码执行的时间超过了一次。

我该如何预防呢?我可以使用其他类似的活动吗?

(在我的datagridview中,我将SelectionMode属性设置为FullRowSelect)



例如当选择更改时,选择是明确的(通过调用clearselection),当DataGridView的RowCount属性增加或减少时,




I've some problem using datagridview..The selectionchange event of Datagridview triggers several times,
Due to these triggers the code written in function executes more then one time .
How can I prevent it ? Is there any other similar event that i can use ?
(In my datagridview I have the SelectionMode property set to FullRowSelect)

for example when the selection is changed ,selection is clear (by calling clearselection ),
when RowCount property of the DataGridView is increase or decrease.

dgvCaseLst1.RowCount = 0
dgvCaseLst1.RowCount = 22
dgvCaseLst1.ClearSelection()
dgvCaseLst1.Rows(Pi_CaseIndex).Selected = True 
 
 Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvCaseLst1.SelectionChanged
 'It triggers 4 times   
 MessageBox.Show("selection changed trigger")
 End Sub

推荐答案

为什么要在事件处理程序中放置事件处理程序?



只需在那里放置你想要的东西。



Why are you putting an event handler inside an event handler?

Just put in there what you want it to do.

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As 
'dgvCaseLst1.RowCount = 0
'dgvCaseLst1.RowCount = 22
'dgvCaseLst1.ClearSelection()
dgvCaseLst1.Rows(Pi_CaseIndex).Selected = True
MessageBox.Show("selection changed trigger")
End Sub





你想要做很多事情

首先你将行数设置为0

然后你将它设置为22

然后你清除选择。

我甚至不确定下一个应该做什么。



你必须决定你想要它做什么。



它的方式它正在完成它们中的4个,这就是为什么你有4个触发器。



如果你需要它来做不同的事情,可以使用if / else或case语句。 />


对我来说所选的更改是f或者对所选行/单元格中的数据执行某些操作。

但是在这里你似乎试图在第一个触发更改事件时更改另一个数据网格视图???



you are trying to do to many things
first you are setting the row count to 0
then you are setting it to 22
then you are clearing the selections.
I'm not even sure what the next one is supposed to do.

You have to make up your mind what you want it to do.

The way it was it was doing all of 4 them thats why you got 4 triggers.

If you need it to do different things use a if /else or a case statement.

for me the selected change is for doing something with the data in the row/cell that was selected.
But here you appear to be trying to change another datagrid view when the change event is fired in the first one ???


这篇关于DataGridView的SelectionChange事件多次触发。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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