0
我有一個datagridview包含用戶的個人信息。我的datagridview正在從數據庫檢索數據填充。在填充datagridview之後,我添加了2個DatagridViewLinkColunm。一個用於編輯所選行的內容,另一個用於保存所做的更改。我想要實現的是,如果用戶選擇一行並單擊「編輯」鏈接標籤,則所選行將被編輯(BeginEdit = true)。但是,如果用戶選擇另一行並且他/她不單擊保存按鈕,則會出現一個MessageBox,詢問用戶是否要保存更改。編輯DataGridView選中行
我試過在CellEnter事件中獲取當前行和當前列,並檢查CellContentClick事件中選中的當前行和前一行,但是我沒有實現我想要的。
下面是代碼...
private void DataGridViewUserList_CellEnter(object sender, DataGridViewCellEventArgs e)
{
currentRow = e.RowIndex;
currentColumn = e.ColumnIndex;
}
private void DataGridViewUserList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (currentColumn == 6)
{
if (rowNumberToBeEdited < 0)
{
rowNumberToBeEdited = e.RowIndex;
goto EnableEditing;
}
if (rowNumberToBeEdited != e.RowIndex)
{
DialogResult dialogResult = MessageBox.Show("Do you want to save the changes you've made", "ACOS-LITE Multi-Application Demo", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
// save data
}
rowNumberToBeEdited = e.RowIndex;
}
EnableEditing:
DataGridViewUserList.CurrentCell = DataGridViewUserList[1, currentRow];
DataGridViewUserList.BeginEdit(true);
DataGridViewUserList.CurrentCell = DataGridViewUserList[2, currentRow];
DataGridViewUserList.BeginEdit(true);
DataGridViewUserList.CurrentCell = DataGridViewUserList[3, currentRow];
DataGridViewUserList.BeginEdit(true);
DataGridViewUserList.CurrentCell = DataGridViewUserList[4, currentRow];
DataGridViewUserList.BeginEdit(true);
}
}