How to bind a checkbox in a datagrid to a database field (C# example required)

J

Johann Blake

Having spent the last 6 hours trying to figure out how to have a
checkbox in a datagrid bind to a field in a database, I'm not getting
anywhere. Virtually all of the postings in this newsgroup and several
others show the examples in VB.NET or show it adding a checkbox during
runtime with the checkbox being added as the first column. Here is
what I really want to do.

After adding a datagrid control to a web form, I then added several
bound columns although no data source is set. The data source gets set
at runtime. I also added a template column and it isn't the first
column in the grid and it is a checkbox column that is is to be
eventually bound to a boolean field in the database. I also set the
Data Binding for the checkbox to have the Checked property attached to
the DataItem.

At runtime, I retrieve a dataset and bind it to the datagrid. The
binding fails with an error "specified cast not valid". Not sure why.
The value returned from the database is a bit value, which I assume
should not be a problem for the Bind method to cast to a boolean,
which is what the Check property of the control expects.

What I did is actually correct. If I remove the data bindings on the
checkbox, the error won't occur, but neither will the checkbox be
bound, so I don't see the value of the field in the database.

Has anyone got a C# example that shows how to bind a checkbox to a
field in a database.

Thanks
Johann Blake

CloserWorlds
www.closerworlds.com

GPS tracking for vehicle, people, cell phones over GSM, GPRS. Uses an
XML web service, supports various mapping software including MapPoint
and OziExplorer, is hardware independent, supports data in multiple
languages and can be adapted by programmers. Uses SQL Server. Built
entirely in C#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top