Updating DataGridView's Record Using TextBox

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.

img1.gif
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

img2.gif

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

img3.gif

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

img4.gif

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

img5.gif

Next Recommended Readings