Datagridview.Cellclick event

Asked

Viewed 459 times

1

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

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

Follow 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");
    }
}

1 answer

1


Just validate which column was clicked and return if it is not the one 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");
    }
}
  • Thank you very much gave it right here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.