Quantcast
Viewing all articles
Browse latest Browse all 12583

Simple DataGridView used to update database table; however...

We're using a DataGridView to apply updates to a database table.  It mostly works fine, but it depends on how we navigate about the DataGridView's cells. 

If we double-click in a cell, change the data, then navigate to another cell by moving the mouse and clicking another cell, it works.  The MessageBox call below displays a "1" and the data in the database has in fact changed.

If we double-click in a cell, change the data, then press the Enter key, it does not work.  The MessageBox call below displays a "0" and the data in the database remains unchanged.

What's up with that?   Thanks!  -- Curt

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

        SqlDataAdapter da;
        DataTable dt;

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(OnEndingEdit);

            SqlConnection myConnection = new SqlConnection("..." );
            myConnection.Open();

            da = new SqlDataAdapter("SELECT * FROM Agencies", myConnection);
            dt = new DataTable();
            da.Fill(dt);

            dataGridView1.DataSource = dt;

            myConnection.Close();
        }

        private void OnEndingEdit(object sender, DataGridViewCellEventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
            builder.GetUpdateCommand();

            MessageBox.Show(da.Update(dt).ToString());
        }
    }


Viewing all articles
Browse latest Browse all 12583

Trending Articles