当我以编程方式选择行时,Datagridview Selectionchanged事件未触发 [英] Datagridview Selectionchanged event not firing when I programmatically select a row

查看:167
本文介绍了当我以编程方式选择行时,Datagridview Selectionchanged事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#winform中有一个datagridview,我希望每当我进入带有datagridview的页面时,应该在第一行选择datagridview而不管之前的位置,我需要选择更改事件触发,以便
当我选择一行时需要发生的代码可能发生。问题是选择更改事件不会在这里触发。


这是我的代码,有人可以帮我理解这里发生了什么吗?


  if(Main.uc1.dgvList.Rows.Count> 0)

            {

                Main.uc1.dgvList.CurrentCell = Main.uc1.dgvList.Rows [0] .Cells [0];

                Main.uc1.dgvList.Rows [0] .Selected = true;

            }




Debra有一个问题

解决方案

我认为你错过了一些东西


1)根据你发布的代码,只有在DataGridView中有任何行时才会触发事件。


2)可能事件被触发了,但你可能要么用一些条件语句过滤掉它,要么selectchanged事件中的代码没有明显的效果。


解决方法


将MessageBox放在方法第一行的selectionchanged事件中,然后运行程序以检查是否显示了messagebox。



I have a datagridview in my c# winform and I want that whenever I come onto the page with the datagridview that the datagridview should be selected on the first row regardless of where it was before and I need the selection changed event to fire so that the code that needs to happen when I select a row can happen. The problem is the selection changed event doesn't fire over here.

This is my code can someone please help me understand what is going on over here?

  if (Main.uc1.dgvList.Rows.Count > 0)
            {
                Main.uc1.dgvList.CurrentCell = Main.uc1.dgvList.Rows[0].Cells[0];
                Main.uc1.dgvList.Rows[0].Selected = true;
            }


Debra has a question

解决方案

I think you are missing out something

1) As per the code you posted, the event will be fired only when there will be any rows in DataGridView.

2) Maybe the event is fired but you might be either filtering it out with some conditional statements or the code in selectionchanged event has no noticeable effect.

Workaround

Place a MessageBox in the selectionchanged event at first line of the method and then run the program to check whether messagebox is displayed.


这篇关于当我以编程方式选择行时,Datagridview Selectionchanged事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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