Ordering error datagridview in c #

0

They could help me to solve this error, it works in a normal datagridview , but in this it does not work, in this datagridview I select any data and it puts it in texbox .

It works in this way so that you can understand me a little better:

When selecting any data from the datagridview, it shows me in the textbox but when wanting to sort the data either by invoice number or by client name, the following error marks me, only the ordering by ID works.

The error shows it to me in the code when I select any data from datagridview to send it to the fields of texbox that belongs this is the code:

 private void dataGridViewCatalogoPartes_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        txtId.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtnumfactura.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[1].Value.ToString();
        txtCliente.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[2].Value.ToString();
        txtparte.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[3].Value.ToString();
        comboBoxTipoMaterial.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[4].Value.ToString();
        txtDescripcionEspañol.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[5].Value.ToString();
        txtDescripcionIngles.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[6].Value.ToString();
        txtCantidad.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[7].Value.ToString();
        txtPrecioUnitario.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[8].Value.ToString();
        txtpallet.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[10].Value.ToString();
        txtobservaciones.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[11].Value.ToString();
        comboBoxPais.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[12].Value.ToString();
        txtpesopallet.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[13].Value.ToString();

    }
    
asked by Daniel 26.10.2017 в 00:13
source

1 answer

1

Good Daniel,

The problem you have is that when you click on the Filter of DataGridView the event CellContentClick is triggered with the Index = -1 and then the exception jumps you.

You should put a condition to control that you do not react when you click on a Filter cell:

private void dataGridViewCatalogoPartes_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        txtId.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtnumfactura.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[1].Value.ToString();
        txtCliente.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[2].Value.ToString();
        txtparte.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[3].Value.ToString();
        comboBoxTipoMaterial.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[4].Value.ToString();
        txtDescripcionEspañol.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[5].Value.ToString();
        txtDescripcionIngles.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[6].Value.ToString();
        txtCantidad.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[7].Value.ToString();
        txtPrecioUnitario.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[8].Value.ToString();
        txtpallet.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[10].Value.ToString();
        txtobservaciones.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[11].Value.ToString();
        comboBoxPais.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[12].Value.ToString();
        txtpesopallet.Text = this.dataGridViewCatalogoPartes.Rows[e.RowIndex].Cells[13].Value.ToString();
    }
}
    
answered by 26.10.2017 / 11:23
source