In this Article, we will learn How to Insert, Update and Delete Record in DataGridView in C# Windows Form Application. In Previous Post, we saw How to Create a simple Windows Form Login Application in C#.
Let's Begin:
1. Create a new Windows Form Application.
2. Create a Database (named as Sample). Add a Table tbl_Record. The following is the table schema for creating tbl_Record.
3. Create a form(named frmMain) and Drop Label, TextBox, Button and DataGridView control from the ToolBox.
Now, Go to frmMain.cs code and add System.Data and System.Data.SqlClient namespace.
frmMain.cs Code:
In the above code, I have created dataGridView1_RowHeaderMouseClick Event for updating and deleting the selected Record. When user click on the Row Header of any row then data present in the cell of the row is stored into the TextBoxes. DisplayData() method used to fill data in DataGridView. Clear() method clears the data present TextBox as well as in ID(int) variable.
Final Preview:
Hope you like it. Thanks.
Let's Begin:
1. Create a new Windows Form Application.
2. Create a Database (named as Sample). Add a Table tbl_Record. The following is the table schema for creating tbl_Record.
3. Create a form(named frmMain) and Drop Label, TextBox, Button and DataGridView control from the ToolBox.
Now, Go to frmMain.cs code and add System.Data and System.Data.SqlClient namespace.
frmMain.cs Code:
using System;
using System.Data;
using
System.Windows.Forms;
using
System.Data.SqlClient;
namespace
InsertUpdateDeleteDemo
{
public partial class frmMain : Form
{
SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated
Security=true;");
SqlCommand cmd;
SqlDataAdapter
adapt;
//ID variable used in Updating and Deleting Record
int ID = 0;
public frmMain()
{
InitializeComponent();
DisplayData();
}
//Insert Data
private void btn_Insert_Click(object sender, EventArgs e)
{
if (txt_Name.Text != ""
&& txt_State.Text != "")
{
cmd = new SqlCommand("insert into tbl_Record(Name,State)
values(@name,@state)", con);
con.Open();
cmd.Parameters.AddWithValue("@name", txt_Name.Text);
cmd.Parameters.AddWithValue("@state", txt_State.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
}
//Display Data in DataGridView
private void DisplayData()
{
con.Open();
DataTable dt=new DataTable();
adapt=new SqlDataAdapter("select * from tbl_Record",con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
txt_Name.Text = "";
txt_State.Text = "";
ID =
0;
}
//dataGridView1 RowHeaderMouseClick Event
private void
dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
txt_Name.Text =
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
txt_State.Text =
dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
}
//Update Record
private void btn_Update_Click(object sender, EventArgs e)
{
if (txt_Name.Text != ""
&& txt_State.Text != "")
{
cmd = new SqlCommand("update tbl_Record
set Name=@name,State=@state where ID=@id",
con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.Parameters.AddWithValue("@name", txt_Name.Text);
cmd.Parameters.AddWithValue("@state", txt_State.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
//Delete Record
private void btn_Delete_Click(object sender, EventArgs e)
{
if(ID!=0)
{
cmd = new SqlCommand("delete tbl_Record
where ID=@id",con);
con.Open();
cmd.Parameters.AddWithValue("@id",ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
}
}
|
Final Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]