.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Insert/update in datagridview

Posted By:satyapriyanayak       Posted Date: June 01, 2013    Points: 40    Category:    URL: http://www.dotnetspark.com  

we will know how to insert/update in datagridview by clicking the cell and then clicking insert/update and delete buttons
 

we will know how to insert/update in datagridview by clicking the cell and then clicking insert/update and delete buttons

 

App.xml file

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

                        <add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\EMP.mdb" />

            </appSettings>

</configuration>

 

 

 

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;

using System.Data.OleDb;

 

namespace Update_delete_datagridview

{

    public partial class Form1 : Form

    {

       OleDbDataAdapter oledbda;

       OleDbCommandBuilder olcb;

       DataTable dataTable;

       BindingSource bindingSource;

       string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

       string str;

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

            OleDbConnection con = new OleDbConnection(ConnectionString);

            con.Open();

            str = "SELECT * FROM student";

         

            oledbda = new OleDbDataAdapter(str, con);

            olcb = new OleDbCommandBuilder(oledbda);

            dataTable = new DataTable();

            oledbda.Fill(dataTable);

            con.Close();

            bindingSource = new BindingSource();

            bindingSource.DataSource = dataTable;

            dataGridView1.DataSource = bindingSource;

            dataGridView1.Columns[0].Visible = false;

        }

 

        private void btnaddupdate_Click(object sender, EventArgs e)

        {

            try

            {

                oledbda.Update(dataTable);

            }

            catch (Exception exceptionObj)

            {

                MessageBox.Show(exceptionObj.Message.ToString());

            }

            MessageBox.Show("Records updated");

 

        }

 

        private void btndelete_Click(object sender, EventArgs e)

        {

            try

            {

                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

                oledbda.Update(dataTable);

            }

            catch (Exception exceptionObj)

            {

                MessageBox.Show(exceptionObj.Message.ToString());

            }

            MessageBox.Show("Records Deleted");

        }

    }

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend