4
Answers

Removing Rows from DataGridView

bagzli

bagzli

12y
4.9k
1
Hello,

I'm having a really messed up problem that I cannot seem to fix.  Any help is appreciated.

So here is the method:
dgvLoadDetails = DataGridView that I'm using.
dtpFrom = DateTimePicker - Where my range starts
dtpTo = DateTimePicker - Where my range ends

Method:
It goes into grid view and grabs the cell that holds the dates and checks by each row if the date in that cell is between the range of Date From and Date To.  If its not between the range of those dates it removes the row.

My Problem, it does all this but it does not removes every row, I have to click run this method few times in order to filter it entirelly.  First time i run it, it removes 2 records, second time it removes 1 record, it never removes all records and I have no idea why.

Data Grid View is linked to a DataSet from Microsoft Access Database file (.mdb)

If you guys can point out my mistake or tell me a better way to do this, basically all I want is to filter the records between certain dates.

            for (int i = 0; i < dgvLoadDetails.RowCount; i++)
            {
                DataGridViewRow row = dgvLoadDetails.Rows[i];
                DateTime currentDate = Convert.ToDateTime(row.Cells[2].Value.ToString());
                if (currentDate.Date < Convert.ToDateTime(dtpFrom.Text) || currentDate.Date > Convert.ToDateTime(dtpTo.Text))
                {
                    dgvLoadDetails.Rows.Remove(row);
                }
            }
Answers (4)