---
title: "How to update a existing DataGridView cell"  
description: "How to update a existing DataGridView cell"  
author: "Mark Mak"  
published: 2016-11-14  
updated: 2016-11-17  
canonical: https://www.mindstick.com/forum/34204/how-to-update-a-existing-datagridview-cell  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# How to update a existing DataGridView cell

Dear All,I am having a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) on how to updating an existing [DataGridView](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) Cell.\

```
Class PTree{   public string model {get; set;}    public int location {get; set;}    public int ploc {get; set;}    public int cloc {get; set;}    public int nloc {get; set;}    public string name {get; set;}    public string desc {get; set;}   public PTree (string v1, int v2, int v3, int v4, int v5, string v6, string v7)    {        this.model = v1;        this.location=v2;        this.ploc=v3;        this.cloc=v4;        this.nloc=v5;        this.name=v6;        this.desc=v7;    }}(model, location, ploc, cloc, nloc,     name, desc)aaaaa,    1,               0,          1,       2,         n1,       n1descaaaaa,    1,                1,         2,      3,          n2,       n2descaaaaa,    1,                2,         3,      9999,   n3,      n3desc   mysql readin {    ps.Add(new PTree(v1, v2, v3, v4, v5, v6,v7));}dataGrideView1.DataSource = ps;
```

\
I would like to [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) the [third](https://yourviews.mindstick.com/story/4957/the-significance-of-the-third-eye-in-hinduism-more-than-just-a-symbol) record 9999 to 4 by just [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) a btnUpdate. How could I do that?\
Thanks!

## Replies

### Reply by Abhishek Srivasatava

Hi Mark

I really apologize I have mentioned nloc instead of cloc in the previous program. I think you need to update you store procedure like this just add your cloc value which may be your primary key value or unique value. I mean to say that use your primary key along with your value which you may need to update.

```
CREATE PROCEDURE insertnewdata(@cloc varchar@NLOC INT) ASBEGIN         DECLARE @model VARCHAR(50)                    DECLARE @location VARCHAR(50)                    DECLARE @ploc VARCHAR(50)                    DECLARE @cloc VARCHAR(50)                    DECLARE @name VARCHAR(50)                    DECLARE @desce VARCHAR(50)                    declare @prenloc varchar(50)                                                 set @prenloc --generate logic to generate nloc                                                 SELECT @model= model, @location= location, @ploc= ploc, @prenloc=nloc, @name= name,@desce = desce FROM table_name WHERE cloc = @cLOC                    update table_name set nloc= @nloc       where cloc= @cloc                     set @ploc=@ploc+1--or generate your required logic                                                set @cloc=@cloc+1 --or generate your required logic                                                                                                INSERT INTO table_name ([model] ,[location],[ploc],nloc,[cloc],[name],[desce])                                                VALUES (@model,@location,@prenloc,@cloc,@name,@desce)          END 
```

There are many ways by which we can handle this situation. But it depends on the user, which path he may need to follow. As stored procedure is a very easy concept to generate logic.

```
cmd.Parameters.AddWithValue("@nloc",dataGridView1.Rows[i].Cells[5].Value.ToString());cmd.Parameters.AddWithValue("@cloc",dataGridView1.Rows[i].Cells[4].Value.ToString());
```

\
\

### Reply by Mark Mak

Thanks Abhishek!\
I think I mislead you. \
This parts is located in dataGridView and these data from a CLASS named PTree.\
\
(model, location, ploc, cloc, nloc, name, desc)\
aaaaa, 1, 0, 1, 2, n1, n1descaaaaa, 1, 1, 2, 3, n2, n2descaaaaa, 1, 2, 3, 9999, n3, n3desc \
First, I have created a database using MySQL and then read these records into CLASS PTree,then I created a List to contains PTree.\
That is beginning of story.\
After I got the List, I would like to do two things with two button.The first button, I will append a new record in the list such that it form a new table like this(model, location, ploc, cloc, nloc, name, desc)aaaaa, 1, 0, 1, 2, n1, n1descaaaaa, 1, 1, 2, 3, n2, n2descaaaaa, 1, 2, 3, 4, n3, n3desc aaaaa, 1, 3, 4, 9999, n3, n4desc\
Second button, will try to insert a new record into the table : for example(model, location, ploc, cloc, nloc, name, desc)aaaaa, 1, 0, 1, 2, n1, n1descaaaaa, 1, 1, 2, 3, n2, n2descaaaaa, 1, 2, 3, 4, n5, new inserted recordaaaaa, 1, 3, 4, 5, n3, n3desc aaaaa, 1, 4, 5, 9999, n3, n4desc\
both ploc, cloc,nloc form a chain. ploc = previous node (0 = begin of chain)cloc = current nloc, nloc = next node (9999 = end of chain)\
I hope I can use my poor English to describe what I want to do.\
Thanks\
MAKMAK

### Reply by Abhishek Srivasatava

Hi Mark

I think nloc field is a unique column, you need to insert a row in the existing table.

As per my understanding, I have prepared the code for this scenario. You need to create a stored procedure which makes programming easy.

```
{SqlConnection con3 = new SqlConnection();            con3.ConnectionString = (@"Data
Source=****; Initial Catalog=****; User ID=***; Password=****");            int i;            i =
dataGridView1.SelectedCells[0].RowIndex;                         SqlCommand cmd = new SqlCommand(“insertnewdata”, con3);           
cmd.Parameters.AddWithValue("nloc",dataGridView1.Rows[i].Cells[5].Value.ToString());cmd.ExecuteNonQuery();}
```

\

```
CREATE PROCEDURE insertnewdata(@NLOC INT) ASBEGIN          DECLARE @model VARCHAR(50)                    DECLARE @location VARCHAR(50)                    DECLARE @ploc VARCHAR(50)                    DECLARE @cloc VARCHAR(50)                    DECLARE @name VARCHAR(50)                    DECLARE @desce VARCHAR(50)                    SELECT @model= model, @location= location,@ploc= ploc,@cloc=cloc, @name= name,@desce = desce FROM table_nameWHERE nloc = @NLOC                 INSERT INTO table_name ([model] ,[location],[ploc],nloc,[cloc],[desce])       VALUES (@model,@location,@ploc,@cloc,@NLOC,@desce)                            END 
```

### Reply by Mark Mak

Hi Abhishek,\
Yes, I forget to state that I have another button. One is update the third record and then append a new record after that \
(model, location, ploc, cloc, nloc, name, desc)aaaaa, 1, 0, 1, 2, n1, n1descaaaaa, 1, 1, 2, 3, n2, n2descaaaaa, 1, 2, 3, 9999, n3, n3desc \
change to =>\
(model, location, ploc, cloc, nloc, name, desc)aaaaa, 1, 0, 1, 2, n1, n1descaaaaa, 1, 1, 2, 3, n2, n2descaaaaa, 1, 2, 3, 4, n3, n3desc aaaaa, 1, 3, 4, 9999, n3, n4desc\
the second button is to insert one record in between this record, such that it can still form a chain by using ploc, cloc and nloc.\
Thanks \

### Reply by Abhishek Srivasatava

Hi Mark,\
Do you want to update specifically third record by clicking update button or do you want to update the record for the selected row?\
\
\
\
\


---

Original Source: https://www.mindstick.com/forum/34204/how-to-update-a-existing-datagridview-cell

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
