Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

c# - How to update access database from datagridview

$
0
0
this ng change
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Client_Control
{
    public partial class Customer_Details : Form
    {
        private DataTable detailTable;
        private string query;
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Environment.CurrentDirectory + "\\MainClientDataFile.mdb;Persist Security Info=False");
        public Customer_Details(string tabName)
        {
            InitializeComponent();
            this.query = string.Format("SELECT ContactName , ContactNumber , Device , Manufacturer, Model," + " Type, PartNo, SerialNo" + " FROM {0}", tabName);
            this.Text = tabName;
            conn.Open();
            detailTable = new DataTable();
            string tableName = tabName;
            OleDbDataAdapter detailAdapter = new OleDbDataAdapter(query, conn);
            if (detailAdapter != null)
            {
                detailAdapter.Fill(detailTable);
            }
            dataGridView1.DataSource = detailTable;
            conn.Close();
        }
        private void editClick(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = false;
        }
        private void saveClick(object sender, EventArgs e)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(query, conn);
            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
            adapter.Update(detailTable);
        }

        private void addClick(object sender, EventArgs e)
        {
            detailTable.Rows.Add();
            dataGridView1.DataSource = detailTable;
        }

        private void removeClick(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("  Click the arrow beside the row you want to delete to highlight it, then click Edit --> Remove Row to delete a row  ");
            }
        }
    }
}

avis  the code i am running but it keeps giving me errors when i try s

If my Question or Comment Helped in anyway, markup as helpful or as answer as the case may be


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>