Insert, Update and Delete Records in a C# DataGridView ||Simple C# Code & Project



#Insert, Update and Delete Records in a C# DataGridView ||Simple C# Code & Project

Coding
SqlConnection con = new SqlConnection(@”Data Source=LAPTOP-FD2L53DGSQLEXPRESS;Initial Catalog=Crud;Integrated Security=True”);
SqlCommand command = new SqlCommand();

private void Form1_Load(object sender, EventArgs e)
{
gridfill();
}
public void gridfill()
{
con.Open();
DataTable table = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(“Select * from Customer_Detail”, con);
da.Fill(table);
con.Close();
dataGridView1.DataSource = table;

}

private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand command = new SqlCommand(“Insert into Customer_Detail(Customer,department,email)values(@Customer,@department,@email)”, con);
command.Parameters.AddWithValue(“@Customer”, textBox1.Text);
command.Parameters.AddWithValue(“@department”, textBox2.Text);
command.Parameters.AddWithValue(“@email”, textBox2.Text);
command.ExecuteNonQuery();
con.Close();
MessageBox.Show(“Customer Detail Insert Successfully”);
gridfill();

}