As you've mentioned DataGridView I'll offer a C# Winforms solution.
First point - if you are using Dates then use a
DateTime
variable NOT a string. You haven't specified any formatting on the
.ToString()
and you have an ambiguous format on your startDate and endDate, so just use the proper type.
Second point - it is always far better to filter the data
before you put it into the DataGridView e.g. by using a WHERE clause in SQL.
However, this solution assumes that you have already populated the DataGridView and want to remove items from it based on a column "griddate".
var startDate = new DateTime(2017,2,25,0,0,0);
var endDate = new DateTime(2017, 2, 25, 0, 0, 0);
for (var i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
if (dataGridView1.Rows[i].Cells["griddate"].Value == null) continue;
var griddate = DateTime.Parse(dataGridView1.Rows[i].Cells["griddate"].Value.ToString());
if (griddate < startDate || griddate > endDate)
dataGridView1.Rows.RemoveAt(i);
}
Note that I start at the
end of the collection of rows and work backwards. Imagine I have rows with indexes 1, 2, 3, 4 ... if I start at the beginning and remove Row 2 then Row 4 will no longer exist, I will have row indexes 1, 2, 3. But by starting at the other end I will have already considered row 4 so I don't care if it gets renumbered.
If you have had rows made invisible elsewhere in the code then removing them is even simpler :
for (var i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
if (!dataGridView1.Rows[i].Visible)
dataGridView1.Rows.RemoveAt(i);
}