Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
i dont have idea about it please help!!

What I have tried:

C#
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();


}
Posted
Updated 16-Jun-22 14:27pm
v2
Comments
PIEBALDconsult 16-Jun-22 12:04pm    
Better not to use integers as IDs.
Also, better not to delete records, but to mark them as inactive.

Don't. ID's should not have to be sequential, they should be unique. If you change ID values, then any data which was not removed relating to the old ID will be associated with the new instead, and that can cause massive data integrity problems which are a nightmare to sort out later.

Why are you even displaying the ID's at all? Generally, the user doesn't need to know what they are, so they are normally a hidden row in a DGV anyway!

Two other things:
1) Indent your code! It's much easier to read, modify, and understand it it's indented - VS will do it for you automatically!

2) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
thankyou @originalGriff for your comment .As, you said it wasnt changed in id column .so, i duplicate the id with another heading and its working.
 
Share this answer
 
Comments
Richard Deeming 17-Jun-22 4:13am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under the solution and post a comment.

Do not post your comment as another "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900