[Webinar] Streamline your web hosting managementRegister Today


How to commit changes when displaying datagrid in C#

Posted on 2008-01-27
Medium Priority
Last Modified: 2013-11-26
I have a datagrid upon which users can make changes. I am now trying to have those changes comitted to the database and I am having trouble determining where in the code I can place the commit command. I cannot seem to find the correct scope of where to put it. Can you please let me know the exact command and location? I was trying to put it in the Exit event but then it does not recognize my dataset object.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace APIMPORT
    public partial class ShowAPIMPORT : Form
        public ShowAPIMPORT()
            DataTable APImportTable = new DataTable();
            string connStr = "Data Source=SQLHOME;Initial Catalog=PLLC;User ID=sa;Password=223";
            string query = "Select BatchID as 'Batch ID',DocumentType as 'Document Type',DocumentDate as 'Document Date',Description,VendorID as 'Vendor ID', DocumentNumber as 'Document Number', convert(decimal(19,2),DocumentAmount) as 'Document Amount',DistributionCode as 'Distribution Code',DistributionDescription as 'Distribution Description',DistributionType as 'Distribution Type',convert(decimal(19,2),DistributionAmount) as 'Amount' from APIMPORT";
            SqlConnection conn = new SqlConnection(connStr);
            SqlDataAdapter rst = new SqlDataAdapter(query, connStr);
                dataGridViewAPIMPORT.DataSource = APImportTable.DefaultView;
                dataGridViewAPIMPORT.Columns[0].Width = 80;
                dataGridViewAPIMPORT.Columns[0].HeaderText = "Batch ID";
                dataGridViewAPIMPORT.Columns[1].Width = 75;
                dataGridViewAPIMPORT.Columns[1].HeaderText = "Doc Type";
                dataGridViewAPIMPORT.Columns[2].Width = 80;
                dataGridViewAPIMPORT.Columns[2].HeaderText = "Doc Date";
                dataGridViewAPIMPORT.Columns[3].Width = 100;
                dataGridViewAPIMPORT.Columns[3].HeaderText = "Description";
                dataGridViewAPIMPORT.Columns[4].Width = 80;
                dataGridViewAPIMPORT.Columns[4].HeaderText = "Vendor ID";
                dataGridViewAPIMPORT.Columns[5].Width = 110;
                dataGridViewAPIMPORT.Columns[5].HeaderText = "Doc Number";
                dataGridViewAPIMPORT.Columns[6].Width = 75;
                dataGridViewAPIMPORT.Columns[6].HeaderText = "Doc Amount";
                dataGridViewAPIMPORT.Columns[6].DefaultCellStyle.Format = "$###,##0.00";
                dataGridViewAPIMPORT.Columns[7].Width = 110;
                dataGridViewAPIMPORT.Columns[7].HeaderText = "Dist Code";
                dataGridViewAPIMPORT.Columns[8].Width = 75;
                dataGridViewAPIMPORT.Columns[8].HeaderText = "Dist Desc";
                dataGridViewAPIMPORT.Columns[9].Width = 75;
                dataGridViewAPIMPORT.Columns[9].HeaderText = "Dist Type";
                dataGridViewAPIMPORT.Columns[10].Width = 80;
                dataGridViewAPIMPORT.Columns[10].HeaderText = "Dist Amount";
                dataGridViewAPIMPORT.Columns[10].DefaultCellStyle.Format = "$###,##0.00";
            catch(Exception ex)
                MessageBox.Show("Exception: " + ex);
                // Close the connection
                if (conn != null)
        private void buttonExit_Click(object sender, EventArgs e)

Open in new window

Question by:rwheeler23

Author Comment

ID: 20754997
I rephrased question title.

Accepted Solution

SwissKnife earned 1000 total points
ID: 20762100
Your problem is, that you use local variables to store the object references.

Move the definition of  the DataTable and the SqlDataAdapter out of the Show function to the scope of the class.

Now you can access the the members in the ButtenExit_Click() handler and commit the changes.

public partial class ShowAPIMPORT : Form
        DataTable APImportTable;
        SqlDataAdapter rst;
        public ShowAPIMPORT()
            APImportTable = new DataTable();
            rst = new SqlDataAdapter(query, connStr);

Open in new window


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question