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++;
}
}
You are associating your Datagridview with some data source (Datasource, Datatable)?
– g-otn
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– Ricardo Pontual
@Ricardopunctual face vlw even by tip worked perfectly, situations we went unnoticed in the face of this problem. but thank you very much.
– Feliphe Savio