제 해결책이 있습니다. 먼저 데이터베이스에서 행을 삭제 한 다음 datagridview를 다시 채 웁니다. 참고 : 데이터베이스 액세스 (oledbcommand, oledbconnection, etc ...)를 사용하여 연결을 변경합니다.
try
{
//connectDB is the procedure to connect to database:
connectDB();
//this delete first the selected rows in database, and then update datagrid
int countRows = dataGridView1.SelectedRows.Count;
for (int i = 0; i < countRows; i++)
{
int currentRow = dataGridView1.SelectedRows[0].Index;
//con.oledbcon is the conecction is the OleDbConnection procedure, miTable = name of your table
OleDbCommand com = new OleDbCommand("DELETE FROM miTable WHERE Id=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), con.oledbcon);
// delete from database
com.ExecuteNonQuery();
// THIS IS THE KEY!!! deselect the last row selected to be able to delete the next row in the next loop
dataGridView1.Rows[currentRow].Selected = false;
}
// all OK
MessageBox.Show("Rows deleted!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
//in case of error
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
//disconnectDB is the procedure to disconnect to database:
disconnectDB();
// and finally, refill datagrid again
loaddatagrid();
}
'가상'모드에서'DataGridView'를 사용하고 있습니까? 그리드에 데이터를 어떻게 묶었습니까? – Junaith
gridview에 데이터 소스가 있습니까? 왜 그것의 데이터 소스에서 원하는 모든 레코드를 삭제하지 마십시오 그리고 당신은 수동으로 gridview 행을 제거하지 않아도 그 UI를 새로 고치려면 gridview.databind()를 호출 할 수 있습니다. – NomNomNom
gridview를 바인딩하지 않았습니다. form.GridTestReport.Rows.Add()를 호출합니다. 가상 모드를 사용하지 않습니다. –