DataTable.Select Not Working In DataGridView events

  • Thread starter Thread starter Tristan
  • Start date Start date
T

Tristan

Hi,

I have a custom DataGridView and the following simple override:

protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);

string filter = "MyBooleanColumn = 1";

DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}

When I inspect the DataTable I can clearly see that it has rows where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!

The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.

If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.

If I put a button on the form and click it, and in the handler put in
the same DataTable.Select, it does return the rows (the row states are
still Added aswell).

Please could someone explain why this strange behaviour is occuring!

Any help appreciated.

Thanks

Tristan.
 
Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.

RobinS.
 
Hi,

Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

Tristan.

Just out of curiosity, have you tried
  string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.

RobinS.



I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
           base.OnCellValueChanged(e);
           string filter = "MyBooleanColumn = 1";
           DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put in
the same DataTable.Select, it does return the rows (the row states are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.

Tristan.- Hide quoted text -

- Show quoted text -
 
Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source down
into the dataset.

If you're not, add one and see if that helps.

myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;

Then do your filter off of the binding source.
myBindingSource.Filter = string.Empty; //remove any current filter
myBindingSource.Filter = ("this column = " + true);

RobinS.
------------------------------------

Hi,

Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

Tristan.

Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.

RobinS.
---------------------------------------"Tristan"



I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put in
the same DataTable.Select, it does return the rows (the row states are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.

Tristan.- Hide quoted text -

- Show quoted text -
 
Hi,

Thanks but I've tried everything you suggest and still have exactly
the same problem! I've also tried using DataViews to do the filtering
but still have the same problem. This leads me to think there is a bug
with the underlying framework unless by design you can't do filtering
inside any of the grid events.

Tristan.


Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source down
into the dataset.

If you're not, add one and see if that helps.

myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;

Then do your filter off of the binding source.
   myBindingSource.Filter = string.Empty;  //remove any current filter
   myBindingSource.Filter = ("this column = " + true);

RobinS.
------------------------------------


Hi,

Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

Tristan.

Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.
RobinS.
---------------------------------------"Tristan"
news:9ba5e619-03eb-4bdb-b8a0-13956c0e95c1@j20g2000hsi.googlegroups.com...
Hi,
I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put in
the same DataTable.Select, it does return the rows (the row states are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.
Thanks
Tristan.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Are you doing an EndEdit on the BindingSource to push the new value down
into the dataset? Try doing that before doing the filter.

RobinS.
GoldMail, Inc.
------------------------------
Hi,

Thanks but I've tried everything you suggest and still have exactly
the same problem! I've also tried using DataViews to do the filtering
but still have the same problem. This leads me to think there is a bug
with the underlying framework unless by design you can't do filtering
inside any of the grid events.

Tristan.


Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source down
into the dataset.

If you're not, add one and see if that helps.

myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;

Then do your filter off of the binding source.
myBindingSource.Filter = string.Empty; //remove any current filter
myBindingSource.Filter = ("this column = " + true);

RobinS.
------------------------------------


Hi,

Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

Tristan.

Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.
RobinS.
---------------------------------------"Tristan"
news:9ba5e619-03eb-4bdb-b8a0-13956c0e95c1@j20g2000hsi.googlegroups.com...
Hi,
I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put in
the same DataTable.Select, it does return the rows (the row states are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.
Thanks
Tristan.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Hi,

Just tried that (apologies, I thought you meant call EndEdit on the
DataGridView) and yes, DataTable.Select then returns the correct rows.
However, the problem with that is that it filters the underlying data
in the grid! This is not want I want to do, I simply want to analyse
the data. Yes, I could remove the filter on the BindingSource after
I've performed the DataTable.Select but I have very large datasets so
that is not ideal. It will also kick off lots of events throughout my
application.

I still can't understand why simply using a DataTable.Select won't
return the correct rows on the first place? It does when invoked
outside of any of the DataGridView events but I would have thought it
should at least work in OnCellValueChanged i.e. when the grid has the
updated value and the DataTable has the updated value aswell. If the
DataTable hasn't got the updated value at this point in time then why
is the updated value shown in the watch windows for the DataTable?!

Thanks.

Tristan.


Are you doing an EndEdit on the BindingSource to push the new value down
into the dataset?  Try doing that before doing the filter.

RobinS.
GoldMail, Inc.

Hi,

Thanks but I've tried everything you suggest and still have exactly
the same problem! I've also tried using DataViews to do the filtering
but still have the same problem. This leads me to think there is a bug
with the underlying framework unless by design you can't do filtering
inside any of the grid events.

Tristan.

Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source down
into the dataset.
If you're not, add one and see if that helps.
myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;
Then do your filter off of the binding source.
myBindingSource.Filter = string.Empty; //remove any current filter
myBindingSource.Filter = ("this column = " + true);
RobinS.
------------------------------------


Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.
RobinS.
---------------------------------------"Tristan"

Hi,
I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put in
the same DataTable.Select, it does return the rows (the row states are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.
Thanks
Tristan.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
I misunderstood, and didn't realize you were trying to do a filter NOT ON
THE GRID, but on the underlying data source.

So just do the EndEdit and don't do the filter.

The problem is that just because it's typed on the screen, it doesn't mean
it's in the dataset. The BindingSource is the glue between the two, and when
you do EndEdit on the binding source, it no longer has to retain the old and
new values in case the user does an Undo, and it pushes the changed values
down into the data source. At least, that's my take on it from how it
appears to work.

So if you put in the EndEdit on the BS (w/o the filter), does it do what you
are trying to achieve?

RobinS.
GoldMail, Inc.
---------------------------------------
Hi,

Just tried that (apologies, I thought you meant call EndEdit on the
DataGridView) and yes, DataTable.Select then returns the correct rows.
However, the problem with that is that it filters the underlying data
in the grid! This is not want I want to do, I simply want to analyse
the data. Yes, I could remove the filter on the BindingSource after
I've performed the DataTable.Select but I have very large datasets so
that is not ideal. It will also kick off lots of events throughout my
application.

I still can't understand why simply using a DataTable.Select won't
return the correct rows on the first place? It does when invoked
outside of any of the DataGridView events but I would have thought it
should at least work in OnCellValueChanged i.e. when the grid has the
updated value and the DataTable has the updated value aswell. If the
DataTable hasn't got the updated value at this point in time then why
is the updated value shown in the watch windows for the DataTable?!

Thanks.

Tristan.


Are you doing an EndEdit on the BindingSource to push the new value down
into the dataset? Try doing that before doing the filter.

RobinS.
GoldMail, Inc.
in message

Hi,

Thanks but I've tried everything you suggest and still have exactly
the same problem! I've also tried using DataViews to do the filtering
but still have the same problem. This leads me to think there is a bug
with the underlying framework unless by design you can't do filtering
inside any of the grid events.

Tristan.

Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source
down
into the dataset.
If you're not, add one and see if that helps.
myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;
Then do your filter off of the binding source.
myBindingSource.Filter = string.Empty; //remove any current filter
myBindingSource.Filter = ("this column = " + true);
RobinS.
------------------------------------


Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.
RobinS.
---------------------------------------"Tristan"

Hi,
I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows
where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put
in
the same DataTable.Select, it does return the rows (the row states
are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.
Thanks
Tristan.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Hi,

Yes, that does work, thanks.

However, if I don't use a BindingSource and do this in
OnCellValueChanged:

Console.WriteLine(((DataTable)this.DataSource).Rows[0]
["MyBooleanColumn"].ToString());

after I have set the value of MyBooleanColumn to True on the first
row, then 'True' is written to the Output Window. So if the updated
value isn't yet in the DataSet then why does 'True' and not 'False'
get written to the Output Window? I can only think that any filtering/
DataTable.Select etc you try and do at this point is based on the old
values, whereas directly accessing the rows gives you the new values.

Tristan.


I misunderstood, and didn't realize you were trying to do a filter NOT ON
THE GRID, but on the underlying data source.

So just do the EndEdit and don't do the filter.

The problem is that just because it's typed on the screen, it doesn't mean
it's in the dataset. The BindingSource is the glue between the two, and when
you do EndEdit on the binding source, it no longer has to retain the old and
new values in case the user does an Undo, and it pushes the changed values
down into the data source. At least, that's my take on it from how it
appears to work.

So if you put in the EndEdit on the BS (w/o the filter), does it do what you
are trying to achieve?

RobinS.
GoldMail, Inc.

Hi,

Just tried that (apologies, I thought you meant call EndEdit on the
DataGridView) and yes, DataTable.Select then returns the correct rows.
However, the problem with that is that it filters the underlying data
in the grid! This is not want I want to do, I simply want to analyse
the data. Yes, I could remove the filter on the BindingSource after
I've performed the DataTable.Select but I have very large datasets so
that is not ideal. It will also kick off lots of events throughout my
application.

I still can't understand why simply using a DataTable.Select won't
return the correct rows on the first place? It does when invoked
outside of any of the DataGridView events but I would have thought it
should at least work in OnCellValueChanged i.e. when the grid has the
updated value and the DataTable has the updated value aswell. If the
DataTable hasn't got the updated value at this point in time then why
is the updated value shown in the watch windows for the DataTable?!

Thanks.

Tristan.

Are you doing an EndEdit on the BindingSource to push the new value down
into the dataset? Try doing that before doing the filter.
RobinS.
GoldMail, Inc.
in message

Thanks but I've tried everything you suggest and still have exactly
the same problem! I've also tried using DataViews to do the filtering
but still have the same problem. This leads me to think there is a bug
with the underlying framework unless by design you can't do filtering
inside any of the grid events.

Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source
down
into the dataset.
If you're not, add one and see if that helps.
myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;
Then do your filter off of the binding source.
myBindingSource.Filter = string.Empty; //remove any current filter
myBindingSource.Filter = ("this column = " + true);
RobinS.
------------------------------------
Hi,
Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.
Tristan.
Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.
RobinS.
---------------------------------------"Tristan"

Hi,
I have a custom DataGridView and the following simple override:
protected override void OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows
where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler put
in
the same DataTable.Select, it does return the rows (the row states
are
still Added aswell).
Please could someone explain why this strange behaviour is occuring!
Any help appreciated.
Thanks
Tristan.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
My guess is the same as yours. One way accesses the original values stored
in the dataset; the other access the new value. Definitely odd.

RobinS.
GoldMail, Inc.
------------------------------------
Tristan said:
Hi,

Yes, that does work, thanks.

However, if I don't use a BindingSource and do this in
OnCellValueChanged:

Console.WriteLine(((DataTable)this.DataSource).Rows[0]
["MyBooleanColumn"].ToString());

after I have set the value of MyBooleanColumn to True on the first
row, then 'True' is written to the Output Window. So if the updated
value isn't yet in the DataSet then why does 'True' and not 'False'
get written to the Output Window? I can only think that any filtering/
DataTable.Select etc you try and do at this point is based on the old
values, whereas directly accessing the rows gives you the new values.

Tristan.


I misunderstood, and didn't realize you were trying to do a filter NOT ON
THE GRID, but on the underlying data source.

So just do the EndEdit and don't do the filter.

The problem is that just because it's typed on the screen, it doesn't
mean
it's in the dataset. The BindingSource is the glue between the two, and
when
you do EndEdit on the binding source, it no longer has to retain the old
and
new values in case the user does an Undo, and it pushes the changed
values
down into the data source. At least, that's my take on it from how it
appears to work.

So if you put in the EndEdit on the BS (w/o the filter), does it do what
you
are trying to achieve?

RobinS.
GoldMail, Inc.
---------------------------------------"Tristan"

Hi,

Just tried that (apologies, I thought you meant call EndEdit on the
DataGridView) and yes, DataTable.Select then returns the correct rows.
However, the problem with that is that it filters the underlying data
in the grid! This is not want I want to do, I simply want to analyse
the data. Yes, I could remove the filter on the BindingSource after
I've performed the DataTable.Select but I have very large datasets so
that is not ideal. It will also kick off lots of events throughout my
application.

I still can't understand why simply using a DataTable.Select won't
return the correct rows on the first place? It does when invoked
outside of any of the DataGridView events but I would have thought it
should at least work in OnCellValueChanged i.e. when the grid has the
updated value and the DataTable has the updated value aswell. If the
DataTable hasn't got the updated value at this point in time then why
is the updated value shown in the watch windows for the DataTable?!

Thanks.

Tristan.

Are you doing an EndEdit on the BindingSource to push the new value
down
into the dataset? Try doing that before doing the filter.
RobinS.
GoldMail, Inc.
------------------------------"Tristan" <[email protected]>
wrote
in message

Thanks but I've tried everything you suggest and still have exactly
the same problem! I've also tried using DataViews to do the filtering
but still have the same problem. This leads me to think there is a bug
with the underlying framework unless by design you can't do filtering
inside any of the grid events.

On 2 Feb, 05:21, "RobinS" <[email protected]> wrote:
Are you using a binding source? If so, try doing an EndEdit() before
checking the dataset; that pushes the changes from the binding source
down
into the dataset.
If you're not, add one and see if that helps.
myGrid.DataSource = myBindingSource;
myBindingSource.DataSource = myDataSet;
Then do your filter off of the binding source.
myBindingSource.Filter = string.Empty; //remove any current filter
myBindingSource.Filter = ("this column = " + true);



Yes already tried that, also get the same problem with string and
integer columns so nothing to do with it being a boolean column.

On 1 Feb, 02:56, "RobinS" <[email protected]> wrote:
Just out of curiosity, have you tried
string filter = "MyBooleanColumn = true";
Assuming your boolean column really IS a boolean, and isn't an int.
RobinS.
---------------------------------------"Tristan"



I have a custom DataGridView and the following simple override:
protected override void
OnCellValueChanged(DataGridViewCellEventArgs
e)
{
base.OnCellValueChanged(e);
string filter = "MyBooleanColumn = 1";
DataRow[] rows =
((DataTable)this.DataSource).Select(filter);
}
When I inspect the DataTable I can clearly see that it has rows
where
MyBooleanColumn is equal to true. However, the DataTable.Select
operation does not return any rows!
The rows in the table have a RowState of Added and I have tried
passing in all the different row states to no avail.
If I call AcceptChanges it works but I can't do this as I need
the
changes to get saved back down to a database.
If I put a button on the form and click it, and in the handler
put
in
the same DataTable.Select, it does return the rows (the row
states
are
still Added aswell).
Please could someone explain why this strange behaviour is
occuring!
Any help appreciated.

Tristan.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Back
Top