Insert, update and delete data using DataGridView


You can use DataGridView to fetch all the data from SQL server 2005 and then perform insert, update and delete directly in gridview then make save changes all at once.(in window application(C#)).

First Create Table in SQL server 2005:
Student
id int not null,
name Varchar(50) null,
Age int not null,

And insert some data...

then try following code....

write following code in page_load event...



//Bind datagrid using dataset and create all command.
public Form2()
{
InitializeComponent();

bs.DataSource = table;
this.dataGridView1.DataSource = bs;

string connstr = "Data Source=SD2-1\\SQLEXPRESS;Initial Catalog=datagrid;Integrated Security=True";
conn.ConnectionString = connstr;

string selectsql = "select * from student";
da.SelectCommand = new SqlCommand(selectsql, conn);

SqlCommand insercommand = new SqlCommand("insert into student(id,name,age) values(@id,@name,@age)", conn);

insercommand.Parameters.Add("@id", SqlDbType.Int, 4, "ID");
insercommand.Parameters.Add("@name", SqlDbType.VarChar, 50, "Name");
insercommand.Parameters.Add("@age", SqlDbType.Int, 4, "Age");
da.InsertCommand = insercommand;

SqlCommand updatecommand = new SqlCommand("update student set name=@name,age=@age where (id=@id)", conn);
updatecommand.Parameters.Add("@id", SqlDbType.Int, 4, "ID");
updatecommand.Parameters.Add("@name", SqlDbType.VarChar, 50, "Name");
updatecommand.Parameters.Add("@age", SqlDbType.Int, 4, "Age");
da.UpdateCommand = updatecommand;

SqlCommand deletecommand = new SqlCommand("delete student where id=@id", conn);
//deletecommand.Transaction = tran;
deletecommand.Parameters.Add("@id", SqlDbType.Int, 4, "ID");
da.DeleteCommand = deletecommand;

conn.Open();
da.Fill(table);
conn.Close();
}

//Save changes all at once on button click event..

private void button1_Click(object sender, EventArgs e)
{

try
{

da.Update(table);
}
catch(Exception ex)
{
label1.text=ex.message;
}
}


Related Articles

More articles: DataGridView Gridview delete Gridview update Gridview insert Gridview operations

Comments

Guest Author: shailesh singh14 Jan 2012

I like it that's great

Guest Author: memtech lodhi11 May 2013

DataGridView control are used very frequently in C#. It has various type of functionality but common function are CRUD operation. So thanks for sharing your

knowledge. There are few other links that have described CRUD (Insert, Delete, Update) operation with good explanation and proper sample. I hope that's helpful for

beginners.


http://www.mindstick.com/Articles/9422cfc8-c2ed-4ec1-9fab-589eb850a863/?Insert%20Delete%20Update%20in%20DataGridView%20with%20DataTable%20in%20C

http://www.dreamincode.net/forums/topic/238727-insert-update-and-delete-records-in-table-with-datagridview-using-c%23/



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: