2015-10-31 157 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 
    } 
} 
相关问题