Error validating if the selected datagridView Index is null

Asked

Viewed 124 times

1

I’m having difficulty feeding a dataGridView with objects, where I need to list properties of another object. as for example:

PRODUCT.CODE, PRODUCT.DESCRIPTION, REQUEST.VALUESARY.

The only solution I could find for such treatment would be the one available in this

http://www.macoratti.net/16/10/c_dgvobj1.htm I don’t know if that would be the ideal way,

because, I assign all my objects to an object list, and I apply in the datasource of dataGridView . where the presentation is certain, But, man PROBLEM starts when I am going to remove an object from the dataGridView , through the function, Remove, from a list.

Lista.Remove(dtGridMaterial.SelectedRows[0].DataBoundItem as Objeto)

Object is removed correctly from dataGridView, but after removal, the event below is triggered:

  private void dtGridMaterial_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((dtGridMaterial.Rows[e.RowIndex].DataBoundItem != null) && (dtGridMaterial.Columns[e.ColumnIndex].DataPropertyName.Contains(".")))
            {
                e.Value = BindProperty(dtGridMaterial.Rows[e.RowIndex].DataBoundItem, dtGridMaterial.Columns[e.ColumnIndex].DataPropertyName);
            }
        }

Where the validation is made if the field that I set in the Datapropertyname property of Datagridview has the point ("."). So I can identify the properties of the other classes, as explained in the link above.

Result, final is the following exception:

inserir a descrição da imagem aqui

I understand that the information I am trying to validate in the event has already been removed by the remove function. however I do not know how to treat the problem. I need help, I thank you.

** COMPLEMENT, Functions used

Remove from Grid

        {
            ObraNegocio obraNegocio = new ObraNegocio(gObra);
           obraNegocio.RemoverMaterialObra(dtGridMaterial.SelectedRows[0].DataBoundItem as MaterialObra);
            gObra.RemoverMaterial(dtGridMaterial.SelectedRows[0].DataBoundItem as MaterialObra); 
        }

Remove from List

public void RemoverMaterial(MaterialObra materialObra)
        {
            ListamaterialObras.Remove(materialObra);
        }

Fill in Grid data

public void PreencherGridMaterial()
        {
            try
            {
                dtGridMaterial.DataSource = null;
                dtGridMaterial.DataSource = gObra.ListamaterialObras;
            }
            catch (Exception ex)
            {
                MessageBox.Show("erro " + ex.Message);
            }
        }

Cellformating event, as I said, to present data from other classes linked to the grid.

 private void dtGridMaterial_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((dtGridMaterial.Rows[e.RowIndex].DataBoundItem != null) && (dtGridMaterial.Columns[e.ColumnIndex].DataPropertyName.Contains(".")))
            {
                e.Value = BindProperty(dtGridMaterial.Rows[e.RowIndex].DataBoundItem, dtGridMaterial.Columns[e.ColumnIndex].DataPropertyName);
            }
        }

Quando acesso via janela imediata

  • only with this piece of code can’t guess... Lista is the DataSource of your grid view? where is the rest of the remove method? submit a [mcve]

  • Exactly what I went through, I assign objects from an object list, and the grid datasource receives this list. , in the remove function, remove a specific object from the list, and assign it again to the datasource, for update C#&#xA; public void RemoverMaterial(MaterialObra materialObra)&#xA; {&#xA; ListamaterialObras.Remove(materialObra);&#xA; }&#xA;&#xA; List<MaterialObra> listaMaterialObra;&#xA;

  • But this is not included in the question... add the snippet where you assign the datasource and update it after removal.

  • I edited the publication, posting all the functions used

No answers

Browser other questions tagged

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