Event DataGridView.CellClick

1

How can I use the DataGridView.CellClick event to click on a single column?

The way I did it in my code if I click any part of the datagrid it does the action, and did not want it to be that way I want to click one and a specific column and perform the action.

Here is my event code:

private void DGW_chklist_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewRow row = this.DGW_chklist.Rows[e.RowIndex];

    this.txt_nota.Text         = row.Cells[1].Value.ToString();
    this.txt_cliente.Text      = row.Cells[2].Value.ToString();
    this.txt_transp.Text       = row.Cells[3].Value.ToString();
    this.txt_volume.Text       = row.Cells[4].Value.ToString();

    ncheklist();
    if (txt_dtinicial.Text != "")
    {
        consultartransp();
    }
    else
    {
        MessageBox.Show("Erro");
    }
}
    
asked by anonymous 22.09.2017 / 14:25

1 answer

1

Just validate which column was clicked and return if it is not what you want.

private void DGW_chklist_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if(e.ColumnIndex != DGW_chklist.Columns["NomeDaColuna"].Index)
        return;

    DataGridViewRow row = this.DGW_chklist.Rows[e.RowIndex];

    this.txt_nota.Text         = row.Cells[1].Value.ToString();
    this.txt_cliente.Text      = row.Cells[2].Value.ToString();
    this.txt_transp.Text       = row.Cells[3].Value.ToString();
    this.txt_volume.Text       = row.Cells[4].Value.ToString();

    ncheklist();
    if (txt_dtinicial.Text != "")
    {
        consultartransp();
    }
    else
    {
        MessageBox.Show("Erro");
    }
}
    
22.09.2017 / 14:32