DataGridView selectionChanged事件多次触发 [英] DataGridView selectionChanged event firing multiple times

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

问题描述

在我的应用程序中,我使用DataGridView来显示列表。当用户在datagridview中选择一条记录时,应该在另一个面板中显示详细信息。为了选择,我被要求使用DataGridView1_SelectionChanged事件。

In my application I am using DataGridView to display the list. When user select a record in the datagridview, it should display details in the other panel. For selection I was asked to use DataGridView1_SelectionChanged event.

DataGridView应该只包含一列,但详细信息显示可能有更多的选择记录的信息(我们可以得到所有的细节通过使用所选的主键值查询形成数据库)。

The DataGridView should contain only one column, but the details display may have more information of the selected record( We can get the all the details form the database by querying with the selected Primary key value).


请告诉可以获取datakey(主键)的所选
DataGridView记录。 DataGridView会根据主键
绑定吗?

Please tell Is it possible to get datakey(Primary key) of the selected DataGridView record. Does DataGridView will bind based on Primary key ?

修改

我已经实现了DataGridView1.Columns [ID]。Visible = false。它运作良好
对于选择,我添加了DataGridView1_SelectionChanged事件。

I have implemented DataGridView1.Columns["ID"].Visible = false. It worked well. For selection I added DataGridView1_SelectionChanged event.


但是DataGridView1_SelectionChanged事件正在多次触发。

But DataGridView1_SelectionChanged event is firing multiple times.


推荐答案

在执行代码之前,可以在selectionchanged事件中使用DataGrid.focused属性:

You can use DataGrid.focused property in selectionchanged event before executing the code like this :

private void dg_SelectionChanged(object sender, EventArgs e)
    {
        if (dg.Focused)
        {
            // your code
        }
    } 

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

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