Populating an Existing Datagridview

Asked

Viewed 80 times

2

Guys I have the following problem, I have a datagrid, in which I would like to insert more lines in it. - When the record is new Grid accepts normal, but when I edit it, it reports to me:

System.Invalidoperationexception: 'Unable to add lines programmatically to the collection of Datagridview lines when the control is associated with data.'

Follow the code showing how it is:

public void setDadosGrid (int id, string descricao)
{      

    for (var i = 0; i <= dgvExames.SelectedRows.Count; i++)
    {

        {
            if (dgvExames.Columns.Count == 0)
            {
                dgvExames.Columns.Add("Id", "Id");
                dgvExames.Columns.Add("Descricao", "Descricao");
            }

            dgvExames.Rows.Add(id,descricao);
            i++;

        }
    }
  • 2

    You are associating your Datagridview with some data source (Datasource, Datatable)?

  • 5

    If Datagrid is connected to a data source, you won’t be able to manually add lines to it that way. Try the following: var row = dgvExames.DataSource.NewRow(); row.Columns["Id"] = id; .... dgvExames.DataSource.Add(row); Tell me if it worked, you may need to cast the Datasource, then put a more detailed answer

  • @Ricardopunctual face vlw even by tip worked perfectly, situations we went unnoticed in the face of this problem. but thank you very much.

No answers

Browser other questions tagged

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