0

I'm struggling with the following. I have a DataTable with dates in it. Dates that can contain several time rows. Now I want to click on a date on the calender and it will put the times records from that day in a listbox so I can then select the time.

I'm stuck and googled around but I'm blind at this point.

The datatable output in gridview:

Datum
-----------------
22-09-14 13:05:00
22-09-14 13:05:18
23-09-14 13:05:36
23-09-14 13:05:54
23-09-14 13:06:12
21-09-14 14:00:01
21-09-14 15:00:01
21-09-14 16:00:01
21-09-14 17:00:01

The code in the calander SelectionChanged event:

        // Create datatable
        DataTable dt = new DataTable();
        string FileName = "C:\\ProjectName\\data\\data.dat";
        var lines = File.ReadAllLines(FileName);

        // Make date column
        dt.Columns.Add("Datum", typeof(DateTime));

        // add rows
        for (int i = 2; i < lines.Count(); i++)
        {
            DataRow dr = dt.NewRow();
            string[] values = lines[i].Split(new char[] { ',' }).Select(x => x.Replace("\"", "")).ToArray();

            for (int j = 0; j < values.Count() && j < 1; j++)


                dr[j] = values[j];

            dt.Rows.Add(dr);
        }

        // Convert selected date
        string currentDate = e.SelectedDates.Count - 1 >= 0 ? e.SelectedDates[e.SelectedDates.Count - 1].Date.ToString("dd-MM-yyyy") : "none";

        // Print selected date to see format
        Label1.Text = currentDate;

        // Create dataview and apply filter
        DataView dv = new DataView(dt);
        dv.RowFilter = "Datum = #" + currentDate + "#";
        dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
        dv.Sort = "Datum DESC";

        // dataview to listbox
        lbSource.DataSource = dv;
        lbSource.DataTextFormatString = "{0:dd-MM-yyyy HH:mm}";
        lbSource.DataTextField = "Datum";
        lbSource.DataValueField = "Datum";
        lbSource.DataBind();

The dv filter gives me:

String was not recognized as a valid DateTime.

Debugging stops with the following line and gives the "String was not recognized as a valid DateTime.":

dv.RowFilter = "Datum = #" + currentDate + "#";

currentdate comes with the correct date: 22-09-2014 (dd-MM-yyyy)

If someone could help me out, thanks a lot :)

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Michiel
  • 1
  • 1
  • 2

1 Answers1

-1

Basically the .rowfilter compare takes the date as Mm/dd/yyyy as comparison. It will see 20/10/2018 as an invalid date. It will work for day being lower than 12.

So in your datatable, the date is stored as dd/mm/yyyy.
Unless you change the date storage format to mm/dd/yyyy, I don't think you can rowsfilter.

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Chelvan Mei
  • 83
  • 1
  • 9