Click here to Skip to main content
14,735,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView (DGV) that is bound to a datasource. I now want to create a procedure to update the data in this DGV once the user is finished editing the data in the cells but if I change the data in a cell and leave the cell the data changes back. I am sure there must be something setting I must set but can't find it.
I als0 would like to add a button to the DGV and then save every row that the user edit. Unfortunately I don't have a clue how to get the event for that button going. Please help!!!:confused::confused::confused: :rolleyes:

I have tried to solve this query in button1 i have binded the grid with data and in button 2 i whatever u edit in the grid and hit button2 those records will be saved in the database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace updategrid
    public partial class Form1 : Form
        public Form1()
            ds = new DataSet();
        DataSet ds;

        private void button1_Click(object sender, EventArgs e)
            //binding grid to data from database
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
            SqlDataAdapter adp = new SqlDataAdapter("select * from details", con); 
            DataTable dt = ds.Tables[0];
            dataGridView1.DataSource = dt;       


        private void button2_Click(object sender, EventArgs e)
            //updating values in grid...
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
            SqlDataAdapter adp = new SqlDataAdapter("select * from details", con); 
            SqlCommandBuilder build = new SqlCommandBuilder(adp);

Do rate my answer once you find it useful

Thanks & Regards
Radix :rose:

I guess you are working using window forms and datagridview.

(1)By default window datagridview allows user to edit its cell value by just "double clicking" on it. Each cell becomes writable of you double click on it. Make sure, property Readonly is not set to true.

(2) There is a property called "DataSource". This is set / get property. When you have updated by cell double click, the data source is automatically updated and you get the updated ( chnaged data ) using DataSource property. DataSource returns an object which is of type "object". Therefore, before using further you need to cast it to appropriate type ( depending upon whether you have used DataTable, DataView object which doing DataBinding operation of your DataGridView. )

<br />
<br />
DataTable dT = (DataTable)dataGridView1.DataSource;<br />
<br />

(3) If you get the Updated data in data table you need to write appropritae code and need to update the database.

(4) Just missed out about adding Button in your post. You can use "DataGridViewButtonColumn" class to add a button column.

<br />
<br />
DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();<br />
            buttonCol.Name = "ButtonColumnName";<br />
            buttonCol.HeaderText = "Header";<br />
            buttonCol.Text = "Button Text";<br />
dataGridView1.Columns.Add(buttonCol); <br />
<br />

Hope it helps you,
Manie Verster 18-May-10 9:22am
Nope, you clearly do not get my point or I was unclear. The cell has for instance the word Roes in and I want to change it to Rose but the moment I exit the cell it changes back to Roes. You see, I am used to using ASP.NET web developer and am new in VB.NET. I am not sure how all the DGV's events fit in to each other. There are so many of them.
Manie Verster 18-May-10 9:25am
Regarding the button, I have already added the button in the DGV and need to add an event to it.
OK, I started screen from anew and did the dgv and datasorces from scratch and please, don't ask me what the difference is 'cause I do not know, but I can edit the datasource and I have a button in my windows form outside of the dgv and I add, update and do everything wonderfully. Thanks for your answers I might come back again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900