How to update a existing DataGridView cell

Total Post:3

Points:21
C# 
C#
 1122  View(s)
Ratings:
Rate this:
Dear All,
I am having a problem on how to updating an existing DataGridView 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,       n1desc
aaaaa,    1,                1,         2,      3,          n2,       n2desc
aaaaa,    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 the third record   9999   to 4 by just click a btnUpdate.
 How could I do that?

Thanks!
  1. Post:24

    Points:168
    Re: How to update a existing DataGridView cell

    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?





      Modified On Apr-11-2018 05:40:34 AM
  1. Post:3

    Points:21
    Re: How to update a existing DataGridView cell

    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,       n1desc
    aaaaa,    1,                1,         2,      3,          n2,       n2desc
    aaaaa,    1,                2,         3,      9999,   n3,      n3desc 

    change to =>

    (model, location, ploc, cloc, nloc,     name, desc)
    aaaaa,    1,               0,          1,       2,         n1,       n1desc
    aaaaa,    1,                1,         2,      3,          n2,       n2desc
    aaaaa,    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 

  1. Post:24

    Points:168
    Re: How to update a existing DataGridView cell

    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
    )
     
    AS
    BEGIN
     
             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
     

      Modified On Apr-11-2018 05:41:06 AM
  1. Post:3

    Points:21
    Re: How to update a existing DataGridView cell

    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,       n1desc
    aaaaa,    1,                1,         2,      3,          n2,       n2desc
    aaaaa,    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,       n1desc
    aaaaa,    1,                1,         2,      3,          n2,       n2desc
    aaaaa,    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,       n1desc
    aaaaa,    1,                1,         2,      3,          n2,       n2desc
    aaaaa,    1,                2,         3,      4,           n5,      new inserted record
    aaaaa,    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

      Modified On Apr-11-2018 05:43:27 AM
  1. Post:24

    Points:168
    Re: How to update a existing DataGridView cell

    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
    )
     
    AS
    BEGIN
             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());



      Modified On Apr-11-2018 05:42:41 AM

Answer