开发者

For loop skipping rows in DataSet

My Application uses For...Next loops to read a spreadsheet into a DataSet and then display information from it based on the results of search conditions (search term and a date range).

I'm having a problem with the data where, if I run a search that should return the first 400 rows in the spreadsheet, I'm only getting around 200 results. I know the search should return the 400 rows because I checked it in the spreadsheet before running the search.

I think my problem might be caused by my date comparisons. I think the problem might be that I'm comparing String value, so if anyone can show me a more efficient way of comparing dates, then that'll be great.

Here's my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If ListBox1.Items.Count <> 0 Then : ListBox1.Items.Clear() : End If
    Dim j As Integer = 0
    If TextBox1.Text.Length = 4 Then
        For i As Integer = 0 To CallData.Tables(0).Rows.Count - 1
            'MsgBox(CallData.Tables(0).Rows(i)(2) & " - FROM( " & DateTimePicker1.ToString & " ) TO( " & DateTimePicker2.ToString & " )")
            If CallData.Tables(0).Rows(i)(3).ToString = TextBox1.Text _
            And CallData.Tables(0).Rows(i)(2).ToString > DateTimePicker1.Value.ToString _
            And CallData.Tables(0).Rows(i)(2).ToString < DateTimePicker2.Value.ToString Then
                ListBox1.BeginUpdate()
                ListBox1.Items.Add(CallData.Tables(0).Rows(i)(2).ToString)
                ListBox1.EndUpdate()
                j = j + 1
            End If
        Next

        Label1.Text = j & " records found."
    En开发者_开发技巧d If
End Sub


When you compare strings to each other it doesn't have anything to do with dates, it compares them alphabetically. You can just cast the object from the row to a date and then compare.

Change:

CallData.Tables(0).Rows(i)(2).ToString > DateTimePicker1.Value.ToString
CallData.Tables(0).Rows(i)(2).ToString < DateTimePicker2.Value.ToString 

to:

CType(CallData.Tables(0).Rows(i)(2), DateTime) > DateTimePicker1.Value
CType(CallData.Tables(0).Rows(i)(2), DateTime) < DateTimePicker2.Value

Btw, I'm no vb expert but I guess this code should do the trick :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜