How do I retrieve data entered in GridView and then save it to the database?

1

On a certain sign-in screen, I have a GridView with some blank lines, so the user would have to enter the information on those lines, and then when clicking the save button, make the INSERT in the bank. p>

How would you do when the cell loses focus by adding the value to a DataTable ? I know there is LostFocus event but this event is activated when GridView loses focus? Or when the cell loses focus?

To create the columns of GridView I'm doing 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:

    
asked by anonymous 31.08.2016 / 22:06

1 answer

1

If your intent is to retrieve the data to be saved to your database after you click the saved button, you can simply scan your% s of% and retrieve field by field using Rows Property it gets a collection that contains all rows from 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 DataGridView you have to pass the name property of your Design as shown below.

In addition, you need to open your connection to the database with your insert or match and convert the fields to the correct type.

    
01.09.2016 / 14:31