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
- }
- }
- }




ĤăRit PatwaPosted May 7, 2018, 10:20 AM
Thank you so much bro ! It helped me aton
Sandeep SharmaPosted Jun 9, 2014, 2:53 AM
@Langa Zungu, try with ms access, its working in my case!!!
Langa ZunguPosted May 25, 2012, 4:12 AM
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