How do you recover data typed in Gridview and then save to the database?

Asked

Viewed 466 times

1

In a certain register screen, I have a GridView with a few blank lines, then the user would have to type the information in those lines, and then when clicking the save button, do the INSERT at the bank.

How would you do when the cell loses focus add value to a DataTable? I know there’s an event LostFocus but this event is activated when the GridView loses Focus? Or when the cell loses Focus?

To create the columns of GridView I’m doing like this:

if (id_crm == 0)
{
   DataTable dat_itens = new DataTable();

    dat_itens.Columns.Add("ITEM", typeof(int));
    dat_itens.Columns.Add("DESCRIÇÃO", typeof(string));
    dat_itens.Columns.Add("DESCRIÇÃO NF", typeof(string));
    dat_itens.Columns.Add("QUANTIDADE", typeof(int));

    for (int i = 1; i < 10; i++)
    {
        DataRow tablerow = dat_itens.NewRow();
        tablerow["ITEM"] = i;
        dat_itens.Rows.Add(tablerow);
    }

    gridControl1.DataSource = dat_itens;
}

Example:

inserir a descrição da imagem aqui

  • Why you have to add the value to a Datatable?

  • It doesn’t have to be a datatable, it can be a list, a dicitionaty, any way you can make an insert in the database

  • And can’t be straight from your grid? The information is all there in table form just go through - there and recover the data 5

  • Hum.. I walk the grid with a data.Reader?

  • Can you post how you are trying to save your data? the part that is on your button, and the tables that you are using. I believe you have a CRM table and an item table, post this to better understand how you could do. This question has an example of how you can traverse your grid.

  • 1

    So, I haven’t even started programming the save button yet. I’ll read this question you sent me, and today I try to make the save button, and put it here. thank you.

  • Managed to solve your problem?

  • Perfect. Really what I needed

Show 3 more comments

1 answer

1


If your intention and recovers the data to be saved in your database after the click on the saved button, you can simply scan your DataGridView and recovers field by field using the Rows Property it gets a collection that contains all lines of DataGridView control.

private void button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in GridItens.Rows)
    {
      var SubCategoriaId = row.Cells["SubCategoriaId"].Value;
      var nomeCat = row.Cells["Nome"].Value;
      var CategoriaId = row.Cells["CategoriaId"].Value;
    }
}

In each row.Cells["SubCategoriaId"].Value; you have to pass the name property of your Design as the image below.

inserir a descrição da imagem aqui

Otherwise you will need to open your connection to the database with your Internet or Internet and convert the fields to the correct type.

Browser other questions tagged

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