how to insert,update and delete in databse in datagridview in windows form

Posted by Aditya1234567 under C# on 3/2/2013 | Points: 10 | Views : 1195 | Status : [Member] | Replies : 4
can any1 plz tell me how to insert,update and delete in datagridview easily to understand for me as i am beginner now.plz help me regarding this which will be easy for me to understand.




Responses

Posted by: Satyapriyanayak on: 3/2/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Form1.cs

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.OleDb;
namespace Insert_Update_Delete_DataGridView
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
bindgrid();
}
private void bindgrid()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataMember="employee";
dataGridView1.DataSource = ds;
con.Close();
}
private void Clear()
{
txt_sno.Text = "";
txt_name.Text = "";
txt_address.Text = "";
txt_age.Text = "";
}

private void btn_edit_Click(object sender, EventArgs e)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
txt_sno.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
txt_name.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
txt_address.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
txt_age.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
}

private void btn_update_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "Update employee set sno=@sno,name=@name,address=@address,age=@age Where sno=@sno";
com = new OleDbCommand(str, con);
com.Parameters.AddWithValue("@sno", txt_sno.Text);
com.Parameters.AddWithValue("@name", txt_name.Text);
com.Parameters.AddWithValue("@address", txt_address.Text);
com.Parameters.AddWithValue("@age", txt_age.Text);
com.ExecuteNonQuery();
MessageBox.Show("Record Successfully Updated");
con.Close();
bindgrid();
Clear();
}

private void btn_delete_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
com = new OleDbCommand();
if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
{
com.CommandText = "DELETE FROM employee WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
MessageBox.Show("Record Successfully Deleted");
}
bindgrid();
}

private void btn_insert_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "Insert into employee(sno,name,address,age) Values (@sno,@name,@address,@age)";
com = new OleDbCommand(str, con);
com.Parameters.AddWithValue("@sno", txt_sno.Text);
com.Parameters.AddWithValue("@name", txt_name.Text);
com.Parameters.AddWithValue("@address", txt_address.Text);
com.Parameters.AddWithValue("@age", txt_age.Text);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Successfully Inserted");
bindgrid();
Clear();
}
}
}



If this post helps you mark it as answer
Thanks

Aditya1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aditya1234567 on: 3/2/2013 [Member] Starter | Points: 25

Up
0
Down
in this oledb means

Aditya1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aditya1234567 on: 3/2/2013 [Member] Starter | Points: 25

Up
0
Down
plz explain me step by step how to do

Aditya1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aditya1234567 on: 3/4/2013 [Member] Starter | Points: 25

Up
0
Down
plz explain me clearly about this which will be easy for me to understand

Aditya1234567, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response