将DataGridView控件中的数据与不断更新的DataTable进行比较(循环问题)

时间:2011-05-03 15:37:49

标签: c# vb.net loops datagridview datatable

我有DataTable通过计时器不断更新数据库中的新数据。该数据最终会重新格式化并显示在DataGridView控件中。

为了防止DataGridView从重新数据绑定中完全刷新(因此,清除选择,重置滚动条,默认排序设置,并在重新更新时使gridview闪烁),我只是删除来自数据网格的旧行DataTable中不再存在,并向自上次更新后DataTable中显示的数据网格添加新行。

为简单起见,我们会将DataGridView称为 LEFT 表,将DataTable称为 RIGHT

我正在寻找一种方法来浏览两个表(左和右),并删除不再在右边的旧行并从左边删除它们。并在RIGHT中查找新项目并将其添加到LEFT。每个表都有一个名为“RecID”的列,其中包含可用于比较的每个条目的唯一ID。

现在,我的问题是......有没有办法在一个循环中完成所有这些?我正在寻找的是伪代码:

<loop through everything>
    if RecID does not exist in LEFT but exists in RIGHT
        add new item to LEFT
    if RecID does exist in LEFT but does not exist in RIGHT
        delete item from LEFT
    if RecID does exist in LEFT and also exists in RIGHT, check each cell in row and update LEFT.. i can handle this part
<end loop>

现在我为每个进程设置了两个嵌套循环,一个用于添加条目,另一个用于删除条目..这需要比我能够在一次传递中执行更多的处理。但我无法想出一个方法来一次性做到这一点。这是我的代码。

'Add new items (alert)
        For Each dr As DataRow In alertTableNew.Rows()
            Dim found As Integer = -1
            For Each gr As DataGridViewRow In DataGridView1.Rows()
                If dr.Item("RecID") = gr.Cells("RecID").Value Then
                    found = dr.Item("RecID")
                    gr.Cells("status").Value = dr.Item("status") 'status may change, update it here
                End If
                If found = -1 Then
                    DataGridView1.Rows.Add( _
                        dr.Item("line"), _
                        dr.Item("acct"), _
                        dr.Item("alarmdescription"), _
                        dr.Item("status"), _
                        dr.Item("name"), _
                        dr.Item("RecID"))
                End If
            Next
        Next

        'Remove expired items (alert)
        For Each gr As DataGridViewRow In DataGridView1.Rows()
            Dim found As Integer = -1
            For Each dr As DataRow In alertTableNew.Rows()
                If Not gr Is Nothing And gr.Cells("RecID").Value = dr.Item("RecID") Then
                    found = dr.Item("RecID")
                    gr.Cells("status").Value = dr.Item("status") 'status may change, update it here
                End If
                If found = -1 Then
                    DataGridView1.Rows().Remove(gr)
                End If
            Next
        Next

我的代码似乎非常低效。任何帮助赞赏。谢谢!

3 个答案:

答案 0 :(得分:1)

我正在提供一个简单的解决方案,我将比较tw2o datagridview单元格值并在第三个datagridview中显示这些值:

        for (int i = 0; i < dtView1.Rows.Count; i++)
        {
            for (int j = 1; j < dtView1.Columns.Count; j++)
            { 
               dtViewResult.Rows[i][j] = (dtView1.Rows[i][j].ToString()) + (dtView2.Rows[i][j].ToString()); 
            }
         }

答案 1 :(得分:0)

好的C#,我认为桌面应用和Web应用都支持您描述的“开箱即用”功能。它被称为DataBinding。 通过DataGridView.DataSourceBindingSource以及DataBind()

在网络上查找示例

答案 2 :(得分:0)

如前所述,数据绑定是解决方案的一部分,但是还有更多内容可以满足您的需求。 DataTable有一个Merge方法,它将现有表和对该表(或具有类似模式的另一个表)的更新组合在一起,试图补偿添加,更改和删除。查看此MSDN文章,了解此方法的说明。

http://msdn.microsoft.com/en-us/library/9dkss51z.aspx