简体   繁体   中英

C# Dynamic DataGridViewCell Click Event

I have one DGV to be clicked and another ones to be created in accordance with this click type and to be displayed in screen for a special job purpose. During creation process of other DGV's some cells (some row-column logic) will be clicked automaticaly with program code. Code below performs AUTO CLICK code part of this action. But I believe there must be a another method (as in Button PerformClick Event Fire) to achive this click action:

            DataGridViewCell dc;
            dc = MyDataGridView[0, 0];
            Rectangle rect = this.MyDataGridView.GetCellDisplayRectangle(0, 0, true);
            MouseButtons b = new MouseButtons();
            MouseEventArgs mev = new MouseEventArgs(b, 1, rect.X, rect.Y, 1);
            DataGridViewCellMouseEventArgs e = new DataGridViewCellMouseEventArgs(0, 0, rect.X + 2, rect.Y + 2, mev);
            MyDataGridView_CellMouseClick(dgvEntegCompany, e);

The question is not very clear. Are you just looking for a CellClick event?

EDIT: So do you need to execute the content of your CellClick? CellClick is an event, so it's triggered at the occurence of cell-click event. It can contain a call to a method, that you can also call independently, "dynamically", if you will.

    // CellClick event
    private void dgItems_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        MyMessage(e.RowIndex, e.ColumnIndex);
    }

    // A user method
    private void MyMessage(Int32 irow, Int32 icol)
    {
        System.Windows.Forms.MessageBox.Show("A cell [" + irow  + ", " + icol  + "] was clicked!");
    }

    // invoking from a button
    private void button1_Click(object sender, EventArgs e)
    {
        MyMessage(this.dgItems.SelectedCells[0].RowIndex, this.dgItems.SelectedCells[0].ColumnIndex );
    }

EDIT 2:

There's no such thing as AUTOCLICK.

  • If you want to execute a code inside the would-be-event, see code above
  • If you want to perform visual changes, such as select a cell, just do it. Note, that this action is dependant on the setting of SelectionMode , it behaves way different for Cell and FullRowSelect , for instance. It's gonna be something like this:
    this.DataGridView1.ClearSelection();
    this.DataGridView1.Rows[0].Cells[3].Selected = True;
  • If you're looking for a way to invoke an action (aka computer AI player), you're most likely have to study multi-threading, ie BackgroundWorker and create a separate thread for controlling a game/simulation/etc. Then the question would be still light-years off

Remark: You have to do much better job expressing your questions, meaning putting much more effort into composing it. You're just wasting community time this way.

I will assume that is what you mean by…

”Fire Event Code for DataGridView Cell Click At Any Column – Row”

… Is that you want to subscribe to the grids event that will fire when any cell in the grid is “clicked” on by the user.

If this is the case… then, subscribe to the grids CellClick or CellMouseClick events.

If you want to subscribe to the grids CellMouseClick event, then the code for that can be done in the designer or you can add the code in the forms constructor after the InitializeComponent(); method is called and it will look something like…

dataGridView1.CellMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseClick);

Then you need to create the event handler method… dataGridView1_CellMouseClick. In this event you can check the DataGridViewCellMouseEventArgs object to see which cell was clicked. The object has a property for the row index and column index of the clicked-on cell. Below is an example.

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
  // This event will fire whenever a user clicks on a cell
  // we can get the row and column index of the "clicked" cell
  MessageBox.Show("Cell at Row: " + e.RowIndex + " Col: " + e.ColumnIndex + "  was clicked");
}

This event will fire each time the user clicks on a cell in the grid.

It is unclear what exactly you are trying to accomplish with the current code.

even these two lines of code would perform the required action:

DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(ColIdx, RowIdx);
MyDataGridvView_CellClick(myDGV, e); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM