Solved

Filtering DataGridView with Date   Cannot perform '=' operation on System.DateTime and System.String

Posted on 2006-11-27
10
1,253 Views
Last Modified: 2008-01-09
Error Message: Cannot perform '=' operation on System.DateTime and System.String

I have a Dataset with date values that look like "11/23/2006" , that format.
I am trying to use a combobox to sort datagridview to only show things concerning tomorrow.

Pretty simple but I keep getting that error message. I have made my column in the dataset a String type

            string dt = DateTime.Today.AddDays(01).ToShortDateString() ;
            if (sortBox.Text == "Due Tomorrow")
            {
                dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = " + dt + "";
            }

            else
            {
            }
0
Comment
Question by:JasonWinn
  • 6
  • 3
10 Comments
 
LVL 9

Expert Comment

by:DrAske
ID: 18026021
>>  dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = " + dt + "";
add single quote
    dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = '" + dt + "'";
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 18033402
hey DrAske,

I gave that a shot but it is not filtering for some reason. I get no error message but it does not filter. I have records in the "dueDate" column for tomorrow (11/29/2006) and everything appears to be correct but not filtering happening

any ideas?

thanks

        private void sortBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string dt = DateTime.Today.AddDays(01).ToShortDateString() ;
         
            if (sortBox.Text == "Due Tomorrow")
            {
                dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = '" + dt + "'";
                this.assingmentListDataGridView.Refresh();
            }

            else
            {
            }
        }
0
 
LVL 9

Expert Comment

by:DrAske
ID: 18034836
>>I gave that a shot but it is not filtering for some reason
That means the first Error Msg has gone.
>> I get no error message but it does not filter
what kind of errors?? compile-time error or it gives you an exception??
>> string dt = DateTime.Today.AddDays(01).ToShortDateString() ;
If you try to print out *dt* you will get something like this "11/29/2006 12:00:00 AM", and I think that's why no filtering happens !! So try this:

private void sortBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string dt = DateTime.Today.AddDays(01).ToShortDateString() ;
           string[] d = dt.split(' '); // tokenize the string
            if (sortBox.Text == "Due Tomorrow")
            {
                dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = '" + d[0]+ "'"; // d[0] is "11/29/2006"
                this.assingmentListDataGridView.Refresh();
            }

            else
            {
          /// ...
            }
        }

try it, and post back the result..
regards,Ahmad;
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 3

Author Comment

by:JasonWinn
ID: 18042098
Hey Ahmad,
I gave it a shot, and no error message, but no filtering.

I made a label.Text = d[0]; and the result was "11/30/2006" so its doing the correct date, but not sure why it isnt actually filtering.

Jason
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 18042962
Maybe there is an easier way? All I would like to be able to do is filter out date's/ranges of my datagridview/dataset.

my goal is to have the user click "filter by things for tomorrow" or "filter for things this week".

maybe there is something easier for filtering with dates?

Jason
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 18042975
Maybe the problem is elsewhere, even doing this not filter out things for 11/30/2006, everything remains visible

            if (sortBox.Text == "Due Tomorrow")
            {
                dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = '11/30/2006'"; // d[0] is "11/29/2006"
                assingmentListDataGridView.Refresh();
     
            }

            else
            {
                /// ...
            }
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 18043015
I am really confused now.... even a simple statement like this is not working.

 If I have this right, this should only display rows containing "hi" in the description column in my DataGridView?

 this.dsAssignment.assignmentsTable.DefaultView.RowFilter = "description = 'hi'";
// Does Not Work
0
 
LVL 9

Expert Comment

by:DrAske
ID: 18043357
OK!! I can't see anything wrong in your code!! but try to add output statement inside the *if* statement to notify you when it is executed. If it doesn't!! then the problem is in the *if* condition.

 if (sortBox.Text == "Due Tomorrow")
            {
                MessageBox.Show("IT'S OK!!");
                dsAssignment.assignmentsTable.DefaultView.RowFilter = "dueDate = '11/30/2006'"; // d[0] is "11/29/2006"
                assingmentListDataGridView.Refresh();
     
            }
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 18046017
Hey DrAske,
I did that and that works fine and the messagebox comes up.

Whats really interesting is, I made label1.Text = d[0], and the label would show 11/30/2006....
im wondernig if it is because my dataset is being loaded from an xml file? the xml file shows 11/30/2006, without any hours, minutes or seconds.

Thanks for your help so far!
jason
0
 
LVL 3

Accepted Solution

by:
bromose earned 500 total points
ID: 18066701
Hi

Your date string in your filter needs to be formatet as:

#" + date.Month + "/" + date.Day + "/" + date.Year + "#"

Regards,
Kaj Bromose
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question