---
title: "datagrid view in C#"  
description: "datagrid view in C#"  
author: "Niki M"  
published: 2016-05-25  
updated: 2016-05-26  
canonical: https://www.mindstick.com/forum/34133/datagrid-view-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 7 minutes  

---

# datagrid view in C#

**this is [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) i have written to [insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net),[update and delete](https://www.mindstick.com/forum/156863/create-xml-file-and-also-update-and-delete-that-xml-file-automatically-in-asp-dot-net-mvc) please help me..update and delete are not working!!!!!**\

```
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient;namespace insertupdatedel{    public partial class Form1 : Form    {                private void label1_Click(object sender, EventArgs e)        {        }        private void Form1_Load(object sender, EventArgs e)        {        }        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\BDWS2036\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30;");        SqlCommand cmd;        SqlDataAdapter adapt;        //ID variable used in Updating and Deleting Record          int ID = 0;          public Form1()        {            InitializeComponent();            DisplayData();        }        private void button1_Click(object sender, EventArgs e)        {            if (textBox1.Text  != "" && textBox2.Text  != "")            {                cmd = new SqlCommand("insert into recorddat(name,state) values(@name,@state)", con);                con.Open();                cmd.Parameters.AddWithValue("@name", textBox1.Text);                cmd.Parameters.AddWithValue("@state", textBox2.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 recorddat",con);              adapt.Fill(dt);              dataGridView1.DataSource = dt;              con.Close();          }     //Clear Data          private void ClearData()          {            textBox1.Text = "";            textBox2.Text = "";            ID = 0;          }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {        }        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)        {            ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());            textBox1.Text  = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();            textBox2.Text  = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();        }        private void button2_Click(object sender, EventArgs e)        {            if (textBox1.Text  != "" && textBox2.Text != "")            {                cmd = new SqlCommand("update recorddat set name=@name,state=@state where ID=@Id",con);                con.Open();                cmd.Parameters.AddWithValue("@Id", ID);                cmd.Parameters.AddWithValue("@name", textBox1 .Text );                cmd.Parameters.AddWithValue("@state", textBox2.Text );                cmd.ExecuteNonQuery();                MessageBox.Show("Record Updated Successfully");                con.Close();                DisplayData();                ClearData();            }            else            {                MessageBox.Show("Please Select Record to Update");            }            /* con.Open();            SqlCommand cmd = new SqlCommand("Update recorddat set name=@name,state=@state Where(id=@id)", con);            cmd.Parameters.AddWithValue("@name", textBox1.Text );            cmd.Parameters.AddWithValue("@state",textBox2 .Text );            cmd.ExecuteNonQuery();            MessageBox.Show("updated......");            con.Close();            DisplayData();            ClearData();*/        }        private void button3_Click(object sender, EventArgs e)        {            if (ID != 0)            {                cmd = new SqlCommand("delete recorddat 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");            }          }      }}
```

\
\
\

## Replies

### Reply by Ashish Awasthi

Problem : When you click button [Update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update)/[Delete](https://www.mindstick.com/forum/33620/how-to-delete-record-from-list-in-mvc-using-ajax). It will not get id which data is update and delete .I have written this code you can apply.

```
private void Update_Click(object sender, EventArgs e)        {            if (textBox1.Text != "" && textBox2.Text != "")            {                if (ID != 0)                {                    cmd = new SqlCommand("update recorddat set name=@name,state=@state where ID=@Id", con);                    con.Open();                    cmd.Parameters.AddWithValue("@Id", ID);                    cmd.Parameters.AddWithValue("@name", textBox1.Text);                    cmd.Parameters.AddWithValue("@state", textBox2.Text);                    cmd.ExecuteNonQuery();                    MessageBox.Show("Record Updated Successfully");                    con.Close();                    DisplayData();                    ClearData();                }            }            else            {                MessageBox.Show("Please Select Record to Update");            }        }        private void Delete_Click(object sender, EventArgs e)        {            if (ID != 0)            {                cmd = new SqlCommand("delete recorddat 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");            }        }
```

\
\


---

Original Source: https://www.mindstick.com/forum/34133/datagrid-view-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
