Hello,
following decription is very easy.
I have a class UC_Main with constructor where I create EventHandler for SelectionChanged event in MainUCGrid control which is standard DataGridView. MainUCGrid control is part of UC_Main class.

public class UC_Main : UserControl
 {
 public UC_Main(Evidence.Nodik nod, DataView columns)
 {
  Initialize();
  MainUCGrid.SelectionChanged += new System.EventHandler(MainUCGrid_SelectionChanged);
 }
...
...
...
}

Then I have another class called UC_Material.

public partial class UC_Material : UC_Main
...
...
...

I have more classes like UC_Material and I use them to create user controls that are placed in main form. MainUCGrid is initialized in UC_Main so this is the same process for any user control.

When I run code everything is almost fine. I click on any row in MainUCGrid in any user control and SelectionChanged event is fired as I would expect.

Problem is that somewhere during first initialization, I mean when I create instance of any user control, this event is fired 7x and I can not find out why.

How can I find a place in my code that fires this event 7x ?? I thought for example that there is 7 rows in DataGridView or 7 columns but it is not, every user control has different number of rows and columns and event is always fired 7x. Problem is that I can see in debugger that SelectionChanged fires but only thing I know is, that sender is MainUCGrid but I do not know why?? I tried to change SelectionChanged to CellClick and this does not fire 7x.

I do not want to bother with the whole code, at least for now.

Recommended Answers

All 2 Replies

This is triggered whenever the selection changes in the DataGridView. You could put constraints. (based upon SelectionMode).

private void dgSelectionChanged(object sender, EventArgs e)
        {
            if (dg.SelectedRows.Count > 0)
            {
            }
        }

Thank you for quick reaction.
I could try it but I would like to solve the reason why this happens. I can not find out why this event is triggered. I thought that normally this event is triggered when you change selection of row (when you click on another row) but obviously not. In my case is event triggered before you can even see the form with DataGridView control.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.