how to update and delete in datagridview in windows form

Posted by Aditya1234567 under C# on 3/12/2013 | Points: 10 | Views : 2468 | Status : [Member] | Replies : 3
how to update and delete the data in datagridview which is being shown in running form in datagridview.




Responses

Posted by: Jayakumars on: 3/12/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
aditya

check this url

http://www.dotnetfunda.com/articles/article29.aspx



Mark as Answer if its helpful to you

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

Posted by: Satyapriyanayak on: 3/12/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/13/2013 [Member] Starter | Points: 25

Up
0
Down
plz tell me coding with sql and any short coding available for this

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

Login to post response