hiding rows where date column doesn't equal today in DataGridView

greggfromnebraska

New member
Joined
Mar 20, 2013
Messages
3
Programming Experience
Beginner
Hi all, quick question here regarding a program I’m making. What I’m trying to do is load the data from a DataTable (dtMeetings) and put it into a DataGridView, where it will show the rows in the DataTable according to descending dates (one of the columns in the DataTable is a date column). The thing is that once it’s in the DataGridView, I ONLY want the rows with the date equaling ‘Today’ to show up (example, on March 21[SUP]st[/SUP] only the rows with the date column equaling the date 3/21/2013 would appear).

I know for absolutely certain that the column itself consists of the DataType ‘Date’ because it’s sorting properly (3/1/2013 shows up as earlier than 1/1/2014 and such) as opposed to the DataType being strings, but I cannot for the life of me get the DataGridView to hide the rows where the date column doesn’t equal ‘Today’. The date column is column #4 (and I am taking the starting with ‘0’ into effect).

Here’s what I have right now:

For Each row In dgvMeetings.Rows
If Date.Compare(Date.Parse(dgvMeetings.Rows(row).Cells(4).ToString()), Date.Parse(Today)) = 0 Then
       dgvMeetings.Rows(row).Visible = False
    End If
Next


The first time I open the form where this should be occurring, nothing happens (it just shows all of the rows), then if I close that form and try opening it again the program crashes with the warning error “Conversion from type 'DataGridViewRow' to type 'Integer' is not valid.” appearing on the If/Then line of code.

I also tried running the program with the following code changes, but the same thing as detailed above happens each time:

For Each row In dgvMeetings.Rows
If Date.Compare(Date.Parse(dgvMeetings.Rows(row).Cells(4).ToString()), Today) = 0 Then
       dgvMeetings.Rows(row).Visible = False
    End If
Next


Or

For Each row In dgvMeetings.Rows
If dgvMeetings.Rows(row).Cells(4).Equals(Today) = True Then 
       dgvMeetings.Rows(row).Visible = False
    End If
Next



I’m so close to finishing this program (this is the very last thing I need done) so it’s getting a bit maddening that this one thing has stopped me dead in my tracks. I’ve been up and down Google and various VB.NET forums for hours today trying to find the answer, but no such luck so far. I’ve tried changing the way I nest the loop (putting the For loop to a Do Until) and the various ways I’ve worded things and played around with the orders, but something just isn’t clicking with this program. Thank you in advance for your time and help!
 
I don't see anything wrong with your code. Sometimes running in debug mode is, well, buggy. Go into the bin\debug folder and run the executable. If it works fine, then I would move on.

Another thing you could try is to use an intermediate variable. Sometimes the debugger does not like long statements. Put your Date.Parse statement into a separate date variable, then compare them.
 

Latest posts

Back
Top