我在数据绑定的DataGridView中有一个复选框,选中该复选框时会打开一个弹出表单。我填写表格并关闭它,表格中的数据填充在行中。但是更改没有保存。
我能够手动编辑单元格,并且这些单元格已成功保存。但是由于某种原因,程序上的代码没有被保存-在确定原因上,我一直没有成功。有人可以帮忙吗?
private void dgvItemsTable_CellValueChanged ( object sender, DataGridViewCellEventArgs e )
{
if ( dgvItemsTable.Columns[e.ColumnIndex].Name == soldColumn &&
(bool)dgvItemsTable.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == true )
{
formSoldItemPopup.FillComboBoxAutoComplete ( dataSetItems, expenseTypeColumn, true );
DialogResult dialogResult = formSoldItemPopup.ShowDialog ();
if ( dialogResult == DialogResult.OK )
{
dgvItemsTable.BeginEdit ( true );
dgvItemsTable.Rows[e.RowIndex].Cells[salesChannelIDColumn].Value = Int32.Parse ( salesChannelID );
dgvItemsTable.Rows[e.RowIndex].Cells[salePriceColumn].Value = formSoldItemPopup.SalePrice;
dgvItemsTable.Rows[e.RowIndex].Cells[saleDateColumn].Value = formSoldItemPopup.SaleDate;
dgvItemsTable.Rows[e.RowIndex].Cells[expensesColumn].Value = formSoldItemPopup.Expenses;
dgvItemsTable.Rows[e.RowIndex].Cells[expenseTypeColumn].Value = formSoldItemPopup.ExpenseType;
dgvItemsTable.Rows[e.RowIndex].SetValues ();
dgvItemsTable.EndEdit ();
UpdateItemsDatabase ();
}
}
}
private void dgvItemsTable_CurrentCellDirtyStateChanged ( object sender, EventArgs e )
{
if ( dgvItemsTable.IsCurrentCellDirty )
{
dgvItemsTable.CommitEdit ( DataGridViewDataErrorContexts.CurrentCellChange );
}
}
private void dgvItemsTable_CellEndEdit ( object sender, DataGridViewCellEventArgs e )
{
UpdateItemsDatabase ();
}
我还尝试获取数据绑定的DataRow并进行更新,但更改仍然不持久。
DataRow dataRow = ( (DataRowView)dgvItemsTable.SelectedRows[0].DataBoundItem ).Row;
dataRow.BeginEdit ();
dataRow[salesChannelIDColumn] = Int32.Parse ( salesChannelID );
dataRow[salePriceColumn] = formSoldItemPopup.SalePrice;
dataRow[saleDateColumn] = formSoldItemPopup.SaleDate;
dataRow[expensesColumn] = formSoldItemPopup.Expenses;
dataRow[expenseTypeColumn] = formSoldItemPopup.ExpenseType;
dataRow.EndEdit ();
dataRow.AcceptChanges ();
更新
因此,我用下面的代码取得了一些成功-但只有当我不包含弹出式表格的两行时,才可以。哪种目的无效-我需要表格中的详细信息来填充行。 :-(
有人知道为什么表单阻止该行成功更新吗?
private void button1_Click ( object sender, EventArgs e )
{
currentColumnBeingEdited = dgvItemsTable.CurrentCell.ColumnIndex;
currentRowBeingEdited = dgvItemsTable.CurrentCell.RowIndex;
string salesChannelID =
dgvSelectedSalesChannel.Rows[dgvSelectedSalesChannel.Rows.Count - 1].Cells[salesChannelIDColumn].Value.ToString ();
formSoldItemPopup.FillComboBoxAutoComplete ( dataSetItems, expenseTypeColumn, true );
formSoldItemPopup.ShowDialog ();
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited].Cells[soldColumn];
dgvItemsTable.Rows[currentRowBeingEdited].Cells[soldColumn].Value = true;
dgvItemsTable.BeginEdit ( false );
dgvItemsTable.EndEdit ();
dgvItemsTable.NotifyCurrentCellDirty ( true );
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited].Cells[salesChannelIDColumn];
dgvItemsTable.Rows[currentRowBeingEdited].Cells[salesChannelIDColumn].Value = salesChannelID;
dgvItemsTable.BeginEdit ( false );
dgvItemsTable.EndEdit ();
dgvItemsTable.NotifyCurrentCellDirty ( true );
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited].Cells[saleDateColumn];
dgvItemsTable.Rows[currentRowBeingEdited].Cells[saleDateColumn].Value = formSoldItemPopup.SaleDate;
dgvItemsTable.BeginEdit ( false );
dgvItemsTable.EndEdit ();
dgvItemsTable.NotifyCurrentCellDirty ( true );
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited].Cells[salePriceColumn];
dgvItemsTable.Rows[currentRowBeingEdited].Cells[salePriceColumn].Value = formSoldItemPopup.SalePrice;
dgvItemsTable.BeginEdit ( false );
dgvItemsTable.EndEdit ();
dgvItemsTable.NotifyCurrentCellDirty ( true );
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited].Cells[expensesColumn];
dgvItemsTable.Rows[currentRowBeingEdited].Cells[expensesColumn].Value = formSoldItemPopup.Expenses;
dgvItemsTable.BeginEdit ( false );
dgvItemsTable.EndEdit ();
dgvItemsTable.NotifyCurrentCellDirty ( true );
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited].Cells[expenseTypeColumn];
dgvItemsTable.Rows[currentRowBeingEdited].Cells[expenseTypeColumn].Value = formSoldItemPopup.ExpenseType;
dgvItemsTable.BeginEdit ( false );
dgvItemsTable.EndEdit ();
dgvItemsTable.NotifyCurrentCellDirty ( true );
dgvItemsTable.CurrentCell = dgvItemsTable.Rows[currentRowBeingEdited + 1].Cells[itemIDColumn];
dgvItemsTable.CommitEdit ( DataGridViewDataErrorContexts.CurrentCellChange );
dgvItemsTable.EndEdit ();
UpdateItemsDatabase ();
}
答案 0 :(得分:0)
好吧,我终于找到了要解决的问题。通过跟踪对DataTable的更改,我发现在DataRow级别接受更改将清除DataTable级别的更改。下面是完美运行的最终代码。
DataTable dataTableSold = dataSetItems.Tables["Table"];
DataRow[] dataRowSold = dataTableSold.Select ( "[" + itemIDColumn + "] = '" + itemID + "'" );
dgvItemsTable.BeginEdit ( false );
dataRowSold[0].BeginEdit ();
dataRowSold[0][salesChannelIDColumn] = salesChannelID;
dataRowSold[0][salePriceColumn] = formSoldItemPopup.SalePrice;
dataRowSold[0][saleDateColumn] = formSoldItemPopup.SaleDate;
dataRowSold[0][expensesColumn] = formSoldItemPopup.Expenses;
dataRowSold[0][expenseTypeColumn] = formSoldItemPopup.ExpenseType;
dataRowSold[0].EndEdit ();
dgvItemsTable.Update ();
UpdateItemsDatabase ();