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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10.   
  11. namespace SqlParameterClass {  
  12.     public partial class Form1: Form {  
  13.         public Form1() {  
  14.             InitializeComponent();  
  15.         }  
  16.         SqlDataAdapter dadptr;  
  17.         DataSet dset;  
  18.         string connstring = "database=student;server=.;user=sa;password=wintellect";  
  19.         private void Form1_Load(object sender, EventArgs e) {  
  20.             dadptr = new SqlDataAdapter("select * from student_detail", connstring); // Specifying SQL statement and Database connection  
  21.             dset = new DataSet(); // creating instance of DataSet  
  22.             dadptr.Fill(dset); // Filling DataSet  
  23.             dataGridView1.DataSource = dset.Tables[0]; // Binding DataGridView with DataSet  
  24.         }  
  25.         int i, j;  
  26.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {  
  27.             i = dataGridView1.CurrentCell.RowIndex;  
  28.             j = dataGridView1.CurrentCell.ColumnIndex;  
  29.             txtcellvalue.Text = dataGridView1.Rows[i].Cells[j].Value.ToString(); // Getting cell value into TextBox  
  30.         }  
  31.         private void btnupdate_Click(object sender, EventArgs e) {  
  32.             SqlCommandBuilder scmb = new SqlCommandBuilder(dadptr);  
  33.             try {  
  34.                 dadptr.Update(dset);  
  35.                 MessageBox.Show("Saved"); // // Showing Success Message  
  36.             } catch (Exception) {  
  37.                 MessageBox.Show("Not Saved"); // Showing Failure Message  
  38.             }  
  39.         }  
  40.         private void btnchange_Click(object sender, EventArgs e) {  
  41.             dset.Tables[0].Rows[i][j] = txtcellvalue.Text; // Set the value of DataSet with the value of TextBox  
  42.         }  
  43.     }  

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 value by TextBox.

img4.gif

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

img5.gif


Similar Articles