hi friends,
i have inserted two buttons(Update, Delete) in datagridview in windowsform application i want perform action on Update, delete buttons for each row in dataGridview Could anyone help me
My code is
private void from_load(object sender, EventArsgs e)
{
this.eMpDETAILTableAdapter.fill(this.eMPDataset.EMP_DETAIL);
{
DataGridViewButtonColumn dgbutton = new DataGridViewButtoncolumn();
dgButton.flatstyle = Flatstyle.Flat;
dgButton.HeaderText = "Button";
dvButton.Name= "Button";
dvButton.UseColumnTextForButtonValue = true;
dvButton.Text = "Update";
dataGridView1.Columns.Add(dvButton);
// in this way i have created another button name "delete"
private void dateGridView1_CellContentclick(object sender, DataGridViewCellEventArgs e )
{
}
thanks
Loading
Satyapriya NayakPosted Jun 25, 2012, 4:30 AM
Try this...
Insert/update in datagridview by clicking the cell and then clicking insert/update and delete buttons
App.xml file
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 Update_delete_datagridview
{
public partial class Form1 : Form
{
OleDbDataAdapter oledbda;
OleDbCommandBuilder olcb;
DataTable dataTable;
BindingSource bindingSource;
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "SELECT * FROM student";
oledbda = new OleDbDataAdapter(str, con);
olcb = new OleDbCommandBuilder(oledbda);
dataTable = new DataTable();
oledbda.Fill(dataTable);
con.Close();
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataGridView1.DataSource = bindingSource;
dataGridView1.Columns[0].Visible = false;
}
private void btnaddupdate_Click(object sender, EventArgs e)
{
try
{
oledbda.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
MessageBox.Show("Records updated");
}
private void btndelete_Click(object sender, EventArgs e)
{
try
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
oledbda.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
MessageBox.Show("Records Deleted");
}
}
}
Thanks
If this post helps you mark it as answer
Jignesh TrivediPosted Jun 25, 2012, 3:19 AM
please refer
http://www.c-sharpcorner.com/UploadFile/718fc8/updating-datagridviews-record-using-textbox/
http://www.c-sharpcorner.com/uploadfile/suthish_nair/datagridview-insert-update-delete-and-retrieve-records-using-stored-procedure/
http://www.dreamincode.net/forums/topic/238727-insert-update-and-delete-records-in-table-with-datagridview-using-c%23/
http://khanrahim.wordpress.com/2010/04/10/insert-update-delete-with-datagridview-control-in-c-windows-application/
hope this will help you.