OnClick event in WinForms DataGridView

I use DataGridView in WinForms, and with this piece of code I assign it to columns and values

dataGrid.DataSource = sourceObject;

Only this row contains all the columns and values ​​in the grid. How to handle the onClick event of a specific row or field. I want to edit a specific element in the grid, but I cannot find a way to send the identifier of the element from the event method.

Is there a DataGridViewEventHandler class that I don't understand?

I also tried to add columns manually as buttons, but I did not find a way to assign it to the action onClick method.

+3
source share
2 answers

"OnClick" DataGridView, . MSDN DataGridView,

MSDN, ,

CellMouseClick

   private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)    {

    System.Text.StringBuilder cellInformation = new System.Text.StringBuilder();
    cellInformation .AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex );
    cellInformation .AppendLine();
    cellInformation .AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
    cellInformation .AppendLine();
    MessageBox.Show(cellInformation.ToString(), "CellMouseClick Event" );
}

CellClick

private void dataGridView1_CellClick(object sender,
    DataGridViewCellEventArgs e)
{

    if (turn.Text.Equals(gameOverString)) { return; }

    DataGridViewImageCell cell = (DataGridViewImageCell)
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

    if (cell.Value == Play)
    {
        // PlaySomething()
    }
    else if (cell.Value == Sing)
    {
        // SingSomeThing();
    }
    else 
    {
     MessagBox.Show("Please Choose Another Value");
    }
}

,

+5

Here, DataGridView. , , CellMouseclick. :

private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
    //Do something

}

, "e", . DataGridViewCellMouseEventArgs. . , . (, DataGridViewCellMouseEventArgs ).

+1

Source: https://habr.com/ru/post/1730746/


All Articles