Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void Form1_Load(object sender, EventArgs e)
        {
           button1.Click += new EventHandler(button1_Click);
        }


private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
        }



C#
void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           if (e.Button == MouseButtons.Right && e.RowIndex == -1 && e.ColumnIndex == -1)
           {
               mCheckedListBox.Items.Clear();
               foreach (DataGridViewColumn c in dataGridView1.Columns)
               {
                   mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
               }
               int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
               mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;
               mCheckedListBox.Width = this.Width;
               mPopup.Show(dataGridView1.PointToScreen(new Point(e.X, e.Y)));
           }
       }



I want to fire "dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;"
on form load.
Posted
Comments
Richard MacCutchan 8-Sep-15 6:05am    
You are not firing anything, you are merely setting event handlers for when the event occurs. I suggest you re-readed the documentation on events and their handlers.
Sathish km 8-Sep-15 6:11am    
please help me how to fire that event?

Hooking up an event - which is what you are doing here:
C#
private void Form1_Load(object sender, EventArgs e)
        {
           button1.Click += new EventHandler(button1_Click);
        }
And here:
C#
private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
        }

Does not cause the event to be fired - it just adds a method to the collection of handlers which will be called when the event does fire. So repeatedly adding the handler in your button Click handler doesn't help anything: each time your user presses the button, it will add another handler so when the event finally fires, it will call the same handler method a number of times.

You should only add the handler once, under normal circumstances.

And you shouldn't be trying to "force" events - instead, restructure your code so that the code inside your CellMouseClick handler is in a separate method, and call that from both your click events:
C#
private void Form1_Load(object sender, EventArgs e)
        {
           button1.Click += new EventHandler(button1_Click);
           dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
        }

private void button1_Click(object sender, EventArgs e)
        {
            DoMyClickAction();
        }

void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           if (e.Button == MouseButtons.Right && e.RowIndex == -1 && e.ColumnIndex == -1)
           {
            DoMyClickAction(e.X, e.Y);
           }
       }

private void DoMyClickAction(int x = 0, int y = 0)
       {
           mCheckedListBox.Items.Clear();
           foreach (DataGridViewColumn c in dataGridView1.Columns)
           {
               mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
           }
           int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
           mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;
           mCheckedListBox.Width = this.Width;
           mPopup.Show(dataGridView1.PointToScreen(new Point(x, y)));
       }
 
Share this answer
 
Comments
Sathish km 8-Sep-15 7:05am    
thks u. working
OriginalGriff 8-Sep-15 7:06am    
You're welcome!
The event is not invoked, because you did not even try to do it. But there is more fundamental reason: you cannot directly invoke any event anywhere but the method of the class which declared the event instance. And as the class System.Windows.Forms.Control is not yours, you cannot to it at all. This is not a problem, but an important .NET fool-proof feature. You just never need it. If you explain what you want to achieve, I'll answer.

In addition to that, your question shows that you have no clue what even is. No, button1_Click is not an event. This is just a method. You make it an event handler only by using the operator +=. You actually do it, so it is used as a handler. The event is System.Windows.Forms.Control.Click.

Not to worry; just learn the delegates and events thoroughly; and soon you will get it all.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900