Christian
Christian

Reputation: 523

How can I know when to update my DB with values from a DataGridView? (Very special case issues)

Technology used: .NET 2008, C#, winforms

So I currently have an issue. I have a DatagridView that contains data pulled from a database. The user is supposed to be allowed to add and Delete rows as they see fit.

My problem, is that I have no idea what event is the best to use to know when a use is finished inputting, so that I can then update the DB.

I've tried using the events "Validated, UserAdded, RowsAdded" but the other problem I'm having, is that this DataGridView pulls from the DB, and if the user navigates to a different transaction (using a binding navigator) some of these events trigger when new data is Fetched, which defeats the purpose, because why would I update the DB right after I pulled data?

I also tried "Leave", but another problem happens when the user clicks on the BindingNavigator to get to the next transaction. It doesn't trigger an "enter" event, therefore not triggering "Leave" on the DataGridViw, thus, the data isn't saved.

Also, once I get the data, it's to be sent to service which does the actually CRUD work, so no fancy db auto update connection stuff.

Does all this make sense?

Upvotes: 0

Views: 134

Answers (1)

Nikki9696
Nikki9696

Reputation: 6348

Testing a DataGridView bound to a DataTable with rows populated run-time and changes "saved" whenever the user makes changes but does not fire when populated by code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataLoad();
        }

        void oDataTable_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Row.RowState == DataRowState.Added)
            {
                MessageBox.Show("A row was added");
            }
            else if (e.Row.RowState == DataRowState.Deleted)
            {
                MessageBox.Show("A row was deleted");
            }
            else if (e.Row.RowState == DataRowState.Modified)
            {
                MessageBox.Show("A row was modified");
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataLoad();
        }

        private void DataLoad()
        {
            // simulate detached data (re)loading
            dataGridView1.DataSource = null; // make sure it's not bound before refresh

            // whatever queried the data and populated a table here
            DataTable oDataTable = new DataTable();
            oDataTable.TableName = "Items";
            oDataTable.Columns.Add(new DataColumn() { ColumnName = "ItemName" });
            oDataTable.Columns.Add(new DataColumn() { ColumnName = "Qty" });

            oDataTable.Rows.Add(new object[] { "Shirt", "1" });
            oDataTable.Rows.Add(new object[] { "Pants", "1" });
            oDataTable.AcceptChanges();

            // rebind gv/table
            dataGridView1.DataSource = oDataTable;

            // add the handler back in for user changes
            oDataTable.RowChanged += new DataRowChangeEventHandler(oDataTable_RowChanged);

        }
    }
}

Upvotes: 1

Related Questions