Rob
Rob

Reputation: 1539

Filter datagridview deadline row between 2 datetimepickers

I have a datagridview that is filled by reading data from an XML file. The datagridview has columns - module, description, deadline and priority. The deadline column is in the following format e.g. 12 February 2012 and it is saved into the XML file as a string.

Next to the DGV I have 2 datetimepickers - startDate and endDate.

The user selects the start and end date and then presses the filter button. Once they do this the DGV should only show rows where the deadline column is between the start and end date.

I thought this would be relatively simple but I think I'm having problems converting. When I press the filter button the else statement is getting initiated.

Here is the code I have so far, sorry if it is a bit of a mess.

  private void scheduleButton_Click(object sender, EventArgs e)

    {

   DateTimePicker startDate = new DateTimePicker();
        DateTimePicker endDate = new DateTimePicker();
        String startDateConverted = Convert.ToString(startDate.Value);
        String endDateConverted = Convert.ToString(endDate.Value);

        foreach (DataGridViewRow dr in TaskTable2.Rows)
        {
            String deadlineRow = dr.Cells["Deadline"].Value.ToString();
            if (startDateConverted<= deadlineRow && deadlineRow<= endDateConverted)
            {
                MessageBox.Show("Display Row"); // display filtered rows here.
            }
            else
            {
                MessageBox.Show("Please ensure start date is set before end date and that there is a task saved between the dates you selected.");
            }
        }
    }

The code in the if statement should be what displays the filtered rows but I'm also not sure how to code that.

If anyone can amend my code and help me to fix this it would be greatly appreciated.

Upvotes: 0

Views: 1697

Answers (2)

Cedric Michel
Cedric Michel

Reputation: 520

replace MessageBox.Show("Display Row");

with MessageBox.Show(dr.ToString());

if it doesn't work give me your code for TaskTable2

Upvotes: 0

Cedric Michel
Cedric Michel

Reputation: 520

for you if

if(starttime<= deadline && deadline<= endtime)

Upvotes: 1

Related Questions