2015-10-31 198 views
0

我用DataGridViewButton刪除Button 並且我有這段代碼用於刪除一行,怎麼可以將這個刪除保存到數據庫?我填寫了dataGridView1DataSet保存DataGridView到數據庫的更改

if (dataGridView1.Rows.Count > 0) 
{ 
    if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].IsNewRow != true) 
    { 
     dataGridView1.Rows.Remove(dataGridView1.CurrentRow);  
    } 
} 

回答

1

假設你Table您有一個名爲id列,你想從基於id數據庫中刪除。試試這個:

private void btnDelete_Click(object sender, EventArgs e) 
{ 
    foreach (DataGridViewRow item in dataGridView1.SelectedRows) 
    { 
     var id = item.Cells[0].Value.ToString();//You can change id and Cells[0] as your need 
     //Write Delete code like this (Delete from Table where id = @id) 
     dataGridView1.Rows.RemoveAt(item.Index);//Remove from dataGridView1 
    } 
} 
相關問題