I am working on a winforms application with three buttons:
- Rent - views all rows on the rent table in my db
- Hire - views all rows on the hire table in my db
- Overdue - filters out rows in Rent and Hire (depending on what you clicked beforehand)
However what I'm seeing is that after the Overdue filter is applied to the hire/rent table, if I click Rent/Hire again (effectively getting rid of the filter), the filter remains. So what I mean is that, if I click Rent, Overdue and then Rent, the filter from Overdue should go away but it remains. How can I resolve this?
The issue I'm currently having is that the datagrid will not refresh (i.e. unfilter) once the Rent or Hire button is pressed (after the Overdue button is pressed).
Let me walk you through it.
1. Rent button is clicked
2. Overdue button is clicked (as you can see DriverNo 1 is filtered out because of the value in DatePaid, which is not overdue)
This is my code so far:
public void visible() { foreach (DataGridViewRow row in dataGridView1.Rows) { row.Visible = true; } } private void viewHire_Click(object sender, EventArgs e) { viewOverdue.ForeColor = Color.Black; viewHire.ForeColor = Color.Red; viewRent.ForeColor = Color.Black; dataGridView1.DataSource = hireBindingSource; dataGridView1.Refresh(); Globals.notesTbl = "hireNotes"; } private void viewRent_Click(object sender, EventArgs e) { viewOverdue.ForeColor = Color.Black; viewHire.ForeColor = Color.Black; viewRent.ForeColor = Color.Red; dataGridView1.DataSource = rentBindingSource; dataGridView1.Refresh(); Globals.notesTbl = "rentNotes"; } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'databaseDataSet.Rent' table. You can move, or remove it, as needed. this.rentTableAdapter.Fill(this.databaseDataSet.Rent); // TODO: This line of code loads data into the 'databaseDataSet.Hire' table. You can move, or remove it, as needed. this.hireTableAdapter.Fill(this.databaseDataSet.Hire); }
public void viewOverdue_Click(object sender, EventArgs e) { viewOverdue.ForeColor = Color.Red; DateTime overdueDate = default(DateTime); DateTime today = DateTime.Now; string odDate = null; if (today.DayOfWeek == DayOfWeek.Monday) { overdueDate = today.AddDays(-12); odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss"); } else if (today.DayOfWeek == DayOfWeek.Tuesday) { overdueDate = today.AddDays(-13); odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss"); } else if (today.DayOfWeek == DayOfWeek.Wednesday) { overdueDate = today.AddDays(-7); odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss"); } else if (today.DayOfWeek == DayOfWeek.Thursday) { overdueDate = today.AddDays(-8); odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss"); } else if (today.DayOfWeek == DayOfWeek.Friday) { overdueDate = today.AddDays(-9); odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss"); } else if (today.DayOfWeek == DayOfWeek.Saturday) { overdueDate = today.AddDays(-10); odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss"); } else if (today.DayOfWeek == DayOfWeek.Sunday) { overdueDate = today.AddDays(-11); odDate = overdueDate.Date.ToString("dd/MM/yyy HH:mm:ss"); } CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource]; manager.SuspendBinding(); DateTime dateBase = DateTime.Parse(odDate); foreach (DataGridViewRow row in dataGridView1.Rows) { if(row.Cells[0].Value != null) { DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString()); row.Visible = (dateRow <= dateBase); } } manager.ResumeBinding(); }