检查重复行并使用BindingSource添加新信息

时间:2018-04-11 17:28:29

标签: .net vb.net

我希望你们都度过了愉快的一周!

我目前在使用下面的代码时遇到了一些问题。基本上我正在搜索现有的行。如果重复行不存在,那么我们添加一个新行,否则我们将信息写入如下:

For Each FindRows As DataRow In dset.Tables("Table1").Rows

    Dim FindMYID As Integer = myBindingSource.Find("MYID", FindRows(0).ToString)
    'If MYID does not exist
    If FindMYID < 0 Then
        myBindingSource.AddNew()
    End If


    CType(myBindingSource.Current, DataRowView)("MYID") = FindRows(0).ToString
    CType(myBindingSource.Current, DataRowView)("Name") = FindRows(1).ToString
    CType(myBindingSource.Current, DataRowView)("P1") = FindRows(2).ToString
    CType(myBindingSource.Current, DataRowView)("P2") = FindRows(3).ToString
    CType(myBindingSource.Current, DataRowView)("P3") = FindRows(4).ToString
    CType(myBindingSource.Current, DataRowView)("P4") = FindRows(5).ToString
    CType(myBindingSource.Current, DataRowView)("P5") = FindRows(6).ToString

    If FindRows(7) IsNot DBNull.Value AndAlso FindRows(8) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(7)) / Convert.ToDouble(FindRows(8)) >= 0.25 Then
            bhkBox1.Checked = False
        Else
            bhkBox1.Checked = True
        End If
    End If

    If FindRows(9) IsNot DBNull.Value AndAlso FindRows(10) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(9)) / Convert.ToDouble(FindRows(10)) >= 0.2 Then
            bhkBox2.Checked = False
        Else
            bhkBox2.Checked = True
        End If
    End If

    If FindRows(15) IsNot DBNull.Value AndAlso FindRows(12) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(15)) / Convert.ToDouble(FindRows(12)) >= 0.4 Then
            bhkBox3.Checked = False
        Else
            bhkBox3.Checked = True
        End If
    End If

    If FindRows(13) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(13)) >= 10 Then
            bhkBox4.Checked = False
        Else
            bhkBox4.Checked = True
        End If
    End If

    If FindRows(14) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(14)) > 0 Then
            bhkBox5.Checked = False
        Else
            bhkBox5.Checked = True
        End If
    End If

    If FindRows(16) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(16)) > 0 Then
            bhkBox6.Checked = False
        Else
            bhkBox6.Checked = True
        End If
    End If

    If FindRows(17) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(17)) > 0 Then
            bhkBox7.Checked = False
        Else
            bhkBox7.Checked = True
        End If
    End If

    If FindRows(18) IsNot DBNull.Value Then 
        If Convert.ToDouble(FindRows(18)) > 0 Then
            bhkBox8.Checked = False
        Else
            bhkBox8.Checked = True
        End If
    End If

    If FindRows(19) IsNot DBNull.Value Then
        If Convert.ToDouble(FindRows(19)) > 0 Then
            bhkBox9.Checked = False
        Else
            bhkBox9.Checked = True
        End If
    End If

    myBindingSource.EndEdit()
Next

问题在于它不会覆盖这些信息。它肯定会找到存在的行,并且它肯定正确地读取信息,但它并没有“保存” - 这意味着当我的DataGridView中显示信息时,应该写入的信息是空白的。有谁知道发生了什么事?

0 个答案:

没有答案