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

查看:40
本文介绍了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).

请告诉是否可以获取所选的数据键(主键)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天全站免登陆