SIGN UP MEMBER LOGIN:    
ARTICLE

Updating DataGridView's Record Using TextBox

Posted by Alok Pandey Articles | ADO.NET in C# January 04, 2012
In this article you will learn to get any DataGridView cell's value into a TextBox and saving the changes into the database.
Reader Level:

Introduction

In this article, I am showing selected values of a DataGridView into TextBox and saving changes into Database.

Open Visual Studio 2010 and create a Windows Forms Application. Add some UI controls and arrange them as in the following figure.


Here I am showing a record in a DataGridView from the database. The database table name is "student_detail" which has some records. Write the following code.

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.SqlClient;
 

namespace
SqlParameterClass
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter dadptr;
        DataSet dset;
        string connstring = "database=student;server=.;user=sa;password=wintellect";
        private void Form1_Load(object sender, EventArgs e)
        {
            dadptr = new SqlDataAdapter("select * from student_detail", connstring);// Specifying SQL statement and Database connection
            dset = new DataSet();// creating instance of DataSet
            dadptr.Fill(dset); // Filling DataSet
            dataGridView1.DataSource = dset.Tables[0]; // Binding DataGridView with DataSet
        }
        int i, j;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
             i = dataGridView1.CurrentCell.RowIndex;
             j = dataGridView1.CurrentCell.ColumnIndex;
             txtcellvalue.Text = dataGridView1.Rows[i].Cells[j].Value.ToString(); // Getting cell value into TextBox
        }      
        private void btnupdate_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder scmb = new SqlCommandBuilder(dadptr);
            try
            {
                dadptr.Update(dset);
                MessageBox.Show("Saved"); // // Showing Success Message
            }
            catch (Exception)
            {
                MessageBox.Show("Not Saved"); // Showing Failure Message
            }
        }
        private void btnchange_Click(object sender, EventArgs e)
        {
            dset.Tables[0].Rows[i][j] = txtcellvalue.Text; // Set the value of DataSet with the value of TextBox
        } 
    }
}

As you have seen, there are two buttons in this application - "Change" and "Update". The "change" button updates the cell value of DataSet with the TextBox value and the "Update" button saves the updated value in the Database. Run the application.

Output



Now click at any cell of DataGridView. Its value will be shown in TextBox.



Update the value of the TextBox and click the "Change" button. It will replace the value of DataGridView cell vlaue by TextBox.



Click the "" button to save changes into Database. It will show success or failure message according to operation.


Login to add your contents and source code to this article
share this article :
post comment
 

Its workng fine bt its nt updating on the database,i get....... sqlselectcommand that does not return any key column information error,i did include da sqlcommandbuilda statement,plz help

Posted by Langa Zungu May 25, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor