0
votes

I have datagridview that contains information from my database. But I need to add new rows with a button.

This error message appears:

Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

 private void pictureBox1_Click(object sender, EventArgs e)
 {
     int rowId = dgvArticulos.Rows.Add(new DataGridViewRow());
 }

I read many posts but they're using DataTable and it doesnt work for my case. Does anybody knows how to solve this issue?

Thanks in advance

1
But what if the idea is to allow the user to add new rows and the save changes in the database??Alonso Ato Neyra
@JohnG I solved my problem. I posted the answer. Thanks you!Alonso Ato Neyra

1 Answers

0
votes

I solve my problem. I created a new List and set the value of my DataSource. After that, I was able to add a row to the new list. It works perfect

List<Documento_Articulo> listaDA = lista;

        listaDA.Add(new Documento_Articulo());

        dgvArticulos.DataSource = null;
        dgvArticulos.DataSource = listaDA;