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);
}
}
bagzliPosted Aug 23, 2012, 12:50 AM
foreach(DataGridViewRow row in dgvLoadDetails.Rows)
{
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);
}
}
That was the solution to that but however I am stuck with same problem from first post. It only removes some of the rows and then when i call it again, it removes all of the rows.
Can you try and explain me a better way to do this? I basically need to have only certain rows show in the data grid view depending on the date range and I need to be able to call back all the rows that use to be there.
I came across another solution and implemented it, however it only works the first time I call it. It won't work again after I rebind the table.
Here is the code:
CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dgvLoadDetails.DataSource];
currencyManager1.SuspendBinding();
foreach(DataGridViewRow row in dgvLoadDetails.Rows)
{
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);
row.Visible = false;
}
}
Here is my bind statement:
this.load_DetailsTableAdapter.Fill(this.recordsDataSet.Load_Details);
So basically I call my method to show the dates, it works the first time then i call my bind statement to refresh the table and all the data come back. Then I call the method again to only show me between certain dates and it crashes with an error saying that data bound with concurrency manager cannot be made invisible, which I don't understand because the method unbinds it every time.
-----------------------------------------------------SOLUTION---------------------------------------
Last Edit (I think):
My crashes are solved once I call .resumeBinding at the end. Not exactly sure how all this works but I think I solved the problem with sheer luck and testing random code! Hurray blind coding!
here is the full Code for those who have the same problem as me:
First we create a currency manager and suspend binding like this:
CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dgvLoadDetails.DataSource];
currencyManager1.SuspendBinding();
foreach(DataGridViewRow row in dgvLoadDetails.Rows)
{
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);
row.Visible = false;
}
}
currencyManager1.ResumeBinding();
Now if you look at that last line it resumes binding, but fear not it doesn't makes those rows visible even though by all logic it should, not sure why it doesn't which is good for us. Then when we wish to make the rows visible again call this method:
foreach (DataGridViewRow row in dgvLoadDetails.Rows)
{
row.Visible = true;
}
and once you add/update/delete a new record and want it updated you can go back to calling the good old fill method and now it doesn't crash.
this.load_DetailsTableAdapter.Fill(this.recordsDataSet.Load_Details);
bagzliPosted Aug 23, 2012, 12:43 AM
Based on what you said that the index is being messed up, I get why so I am trying to do a for each loop but can't get it working either. I tried working backwards but it wasn't doing anything not sure why.
Can you help me put a statement together with a foreach loop?
Error that I get: foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator'Records\Form1.cs
Sukesh MarlaPosted Aug 22, 2012, 11:36 PM
Javeed M ShaikhPosted Aug 22, 2012, 10:24 PM
try this..