我有一个包含3个Gridviews的Windows窗体(参见屏幕截图)。其中一个网格视图在空单元格中获取一个新值,另外两个每个都获得一个新行。单击保存按钮时,我想更新具有添加值的表,并将新行插入到其他两个表中。我很难搞清楚正确的代码。现在我有:
private void buttonSave_Click(object sender, EventArgs e)
{
DataTable table1 = alereDataSet.Tables["immaster"];
DataTable table2 = uPCDataSet.Tables["UPC"];
DataTable table3 = hangtagDataSet.Tables["upccode"];
DialogResult dr = MessageBox.Show("Are you sure you want to commit the values to the databases?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
try
{
immasterTableAdapter.Update(table1.Select(null, null, DataViewRowState.CurrentRows));
upccodeTableAdapter.Update(table3.Select(null, null, DataViewRowState.Added));
uPCTableAdapter.Update(table2.Select(null, null, DataViewRowState.Added));
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
但是当我点击保存没有任何反应。我有一种感觉,这是因为我的表格更新方法。三个表中有两个是VFP表,第三个是SQL表。我需要帮助的是搞清楚更新命令。两个VFP表不会自动创建Update方法,SQL表的代码是:
UPDATE UPC
SET UPCBarcode = @UPCBarcode, UPCNumber = @UPCNumber, ItemNumber = @ItemNumber, Itemdescrip = @Itemdescrip
WHERE (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND
(@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
(UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (ItemNumber = @Original_ItemNumber) AND
(@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
(UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (ItemNumber = @Original_ItemNumber) AND
(@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
(UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (@IsNull_ItemNumber = 1) AND
(ItemNumber IS NULL) AND (Itemdescrip = @Original_Itemdescrip) OR
(UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND
(Itemdescrip = @Original_Itemdescrip) OR
(UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (ItemNumber = @Original_ItemNumber) AND
(Itemdescrip = @Original_Itemdescrip) OR
(UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (ItemNumber = @Original_ItemNumber) AND
(Itemdescrip = @Original_Itemdescrip)
不确定我应该发布什么。请询问您认为可能相关的更多代码。
答案 0 :(得分:0)
好吧,我想经过一个星期的无所事事,我终于能够搞清楚了。更新SQL表非常简单,因为它在创建数据集时会创建自己的UPDATE方法。 对于VFP表,我必须编写自己的UPDATE命令,第一个表是
UPDATE immaster
SET item = ?, descrip = ?, upccode = ?
WHERE (item = ?)
并且第二个VFP表是
UPDATE upccode
SET item = ?, upccode = ?, memoupc = ?
WHERE (item = ?)
然后我必须为Save按钮编写事件处理程序:
//Save the data from the gridviews back to the respective tables
private void buttonSave_Click(object sender, EventArgs e)
{
//Declare which tables get updated
DataTable table1 = alereDataSet.Tables["immaster"];
DataTable table2 = uPCDataSet.Tables["UPC"];
DataTable table3 = hangtagDataSet.Tables["upccode"];
DialogResult dr = MessageBox.Show("Are you sure you want to commit the values to the databases?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
try
{
//Update immaster with the modified data
immasterTableAdapter.Update(table1.Select(null, null, DataViewRowState.ModifiedCurrent));
dataGridView1.Refresh();
//Insert a new row in the upccode table with the values from the cells in the last row
upccodeTableAdapter.Insert(dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[0].Value.ToString(), dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[1].Value.ToString(), ("Added " + System.DateTime.Now));
dataGridView3.Refresh();
//Insert a new row in UPC table with the values from the cells in the last row
uPCTableAdapter.Insert(dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[3].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[2].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[0].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[1].Value.ToString());
dataGridView2.Refresh();
clearTextboxes();
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}