简体   繁体   English

将数据表行的行状态设置为已删除

[英]Set RowState of a DataTable row to deleted

I have an UltraGrid in my project, and I have an update function on the form to update the data in the database where the data is matching. 我的项目中有一个UltraGrid ,并且表单上有一个更新功能,用于更新数据匹配的数据库中的数据。

The data in the grid is being stored as a DataTable . 网格中的数据被存储为DataTable If a row of existing data is deleted from the UltraGrid , I want to be able to set the RowState of that row in the DataTable to 'RowState.Deleted , so that in the Update function I can check the RowState`, and if it's a deleted row, then deleted it, otherwise, update the data. 如果从UltraGrid删除了一行现有数据,我希望能够将DataTable中该行的RowState设置为'RowState.Deleted , so that in the Update function I can check the RowState`,如果它是删除行,然后将其删除,否则,更新数据。

How can I go about doing this? 我该怎么做呢? So far, I have the code below, but the count of rows being returned is 1 (or, the current number of rows in the grid) and not 2 (the number there was before I deleted one row). 到目前为止,我有下面的代码,但是返回的行数为1(或网格中的当前行数),而不是2(删除我的一行之前的行数)。

How and where in the code, do I set the RowState of the deleted row to be RowState.Deleted ? 如何在代码中以及在代码中的何处将已删除行的RowState设置为RowState.Deleted Is there an alternative way of doing it using the UltraGrid ? 是否有使用UltraGrid的替代方法?

dsProducts.Tables.Add(commDt.Copy) -- commDt is the DataTable linked to the UltraGrid
tr = con.BeginTransaction(

  For Each dr As DataRow In dsProducts.Tables(0).Rows
    If dr.RowState = DataRowState.Deleted Then
     Try

The grid will call mark the row as deleted (setting the RowState) and if you are using a TableAdapter then you can simply call Update on the table adapter passing the DataTable to the TableAdapter and it will do the updates in the database. 网格将调用将行标记为已删除(设置RowState),如果您使用的是TableAdapter,则可以简单地在表适配器上调用Update,将DataTable传递给TableAdapter,它将在数据库中进行更新。 For example: 例如:

ultraGrid1.UpdateData()
Me.dbRowsTableAdapter.Update(Me.testDataSet)

If you must manually handle updates and you want to get the deleted rows, you could call Select on the DataTable passing in DataViewRowState .Deleted for the third parameter. 如果必须手动处理更新,并且要获取已删除的行,则可以在DataTable上调用Select ,并将第三个参数传递给DataViewRowState .Deleted。

Solved it. 解决了。

  • Declare a DataTable at the top of the class 在类的顶部声明一个DataTable

  • On the BeforeRowsUpdate method, insert a new row into the DataTable , which are the values of the row being deleted. BeforeRowsUpdate方法上,将新行插入到DataTable ,这是要删除的行的值。

  • Optionally, include a confirmation box to ensure they wish to delete the row. (可选)包括一个确认框,以确保他们希望删除该行。 If they cancel the deletion, remove the row from the DataTable . 如果他们取消删除,请从DataTable删除该行。

  • This DataTable can then be used for deleting rows in the UPDATE query, whilst the grid DataSource can handle the UPSERTS 然后,可以使用此DataTable删除UPDATE查询中的行,而网格DataSource可以处理UPSERTS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM