How can i insert, update or delete option in gridview in desktop application?

Posted by Parag under C# on 11/24/2008 | Views : 4431 | Status : [Member] | Replies : 5
I make 1 DESKTOP APPLICATION.

Now i want to insert, update or delete my data in grid view. but i have no any idea about desktop application.

parag barot


Responses

Posted by: Jayakumars on: 9/30/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

refer this

http://khanrahim.wordpress.com/2010/04/10/insert-update-delete-with-datagridview-control-in-c-windows-application/



Mark as Answer if its helpful to you

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

Posted by: Jayakumars on: 9/30/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Please mark as answer Resolved Posts if it helpful to you.

Mark as Answer if its helpful to you

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

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\EMP.mdb" />
</appSettings>
</configuration>


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

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

Posted by: Poster on: 11/24/2008 [Member] Starter

Up
0
Down
Please follow below link.

http://www.eggheadcafe.com/community/aspnet/7/10052319/formatting-of-gridview-in.aspx

Thanks

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

Posted by: Vuyiswamb on: 12/11/2008 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Chec k what Vuyiswa has done here

http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=3942021



Thank you for posting at Dotnetfunda
[Administrator]

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

Login to post response