how to arrange the id after delete the row from datagridview in c#

denesh neupane 121 Reputation points
2022-06-15T02:24:21.093+00:00

i want to re-arrange the id number after delete the selected row from the datagridview .
if id was 1,2,3,4,5 then after delete the third row output should be 1,2,3,4.

please help!!

private void DeleteRow_Click(object sender, EventArgs e)

    {  
        if(DataGridView.SelectedRows.Count > 0)  
        {  

            using (SqlCommand cmd = con.CreateCommand())  
            {  
                int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value);  
                cmd.CommandText = "Delete from [Table] where id='" + id + "'";  


                con.Open();  
                cmd.ExecuteNonQuery();  
                con.Close();  
                DisplayData();  

                DataGridView.Refresh();  
                commentbox.Text = "削除しました ";  
                commentbox.BackColor = Color.Yellow;  

            }  

        }  
        else  
        {  
            commentbox.Text = "削除する生を選択してください ";  
            commentbox.BackColor = Color.Yellow;  

        }  

    }  

//display

private void DisplayData()

    {  
        con.Open();  
        DataTable dt = new DataTable();  
        SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM [Table] ", con);  
        adp.Fill(dt);  
        DataGridView.DataSource = dt;  


        con.Close();  


    }  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,321 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-06-15T05:52:26.547+00:00

    @denesh neupane , Welcome to Microsoft Q&A, you could try to update every row's id to arrange the id after deleting the row in the datagirdview.

    Code:

    211490-image.png

    211497-image.png

    Note: I can not show the code use the code block so I used picture to replace it.

    You could refer to the txt file.
    211498-code.txt

    Result:
    211505-1.gif
    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Viorel 112.8K Reputation points
    2022-06-15T05:23:10.757+00:00

    It is not clear why you cannot avoid such renumbering.

    Try something like this:

    using (SqlCommand cmd = con.CreateCommand())  
    {  
       cmd.CommandText = "update t set id = new_id from (select *, row_number() over (order by id) as new_id from [Table]) t";  
       con.Open();  
       cmd.ExecuteNonQuery();  
       con.Close();  
    }  
    

    Maybe you do not need this.