Update cell value in Datagridview in C#

Asked

Viewed 848 times

1

Hello!

When clicking a button, I need you to update the value of a cell in the datagridview.

follows the code I tried but didn’t work.

  private void btnAcesso_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in dgvLista.SelectedRows)
        {
            dgvLista.Rows[item.Index].Cells["acesso"].Value = "S";
        }
    }

You have to update from the selected line.

Note: Enter the foreach but does not update.

Thank you.

Grid completion:

 var source = new BindingSource();
            source.DataSource = lstMenuAux;
            dgvLista.DataSource = source;
  • You used the datagridview.DataSource to insert the data into the grid?

  • Yes, with the help of DataBinding

  • You should change the model you set in the datasource, for example ItemDataGridView obj = (ItemDataGridView)dgvLista.CurrentRow.DataBoundItem; obj.acesso = "S";, recalling that Itemdatagridview is the class you used to set in the datasource

  • I don’t understand. where you got that one from ItemDataGridView does not appear here

  • Replace Itemdatagridview with the class you used to play in the datasource of datagridview

  • Post the code of how you fill in the datagridview

  • Okay, posted, edited in question

  • What is the type of lstMenuAux?

  • It is an object.. a model.. a list of objects

  • Yes, young man, I know that. I want you to type the name of the guy here, so I can write an answer.

  • Sorry... List<Menumodel>

  • Try to exchange Itemdatagridview for Menumodel

  • I tried Marco, but it didn’t work.. It’s in the question update

  • Marco. It worked out! Thank you!

Show 9 more comments
No answers

Browser other questions tagged

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