Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set the value of a checkbox in a datagrid?

Status
Not open for further replies.

KidHoss

Programmer
May 22, 2000
27
US
I get the current state of a checkbox item in a templatecolumn in a datagrid by creating a checkbox item and do findcontrol (dgItem.FindControl("chkExportItemSelected"))

After reading stored data from a database I want to loop through the datagrid and set the checkboxes as related to the data from the database.

My question is how do I set the checked state of the checkbox as I loop through the datagrid?
 
Use the Itemdatabound Event of the grid. Then get a reference to the checkbox like you have done already. then you can set the checkbox1.checked = TRUE.
If you are not doing this on a post back you can loop through the grid like this:
Dim dgi As DataGridItem
For Each dgi In datagrid1.Items

Next

Jim
 
I am not reacting to the databinding of data to this datagrid. I am reacting to a button being pressed that causes a set of data to be retrieved from a database. I loop through the dataset from the database read and as I find a match in the datagrid, I want to set the checked value of the corresponding checkbox to true. How do I reference the datagrid checkbox to set it's value, not test it's value?

Thanks,
 
Then Use the second method I posted above in the button click event.
 
That is how I loop through the datagrid items.... but how do I set the checkedd state of the embedded checkbox? I know how to check to see if teh checjk box is check or not. I know it will not be checked. I want to check it based on data read from rthe database. Pwerhaps I am not understanding what you are telling me, but your reference does not mention setting the checked state of teh checkbox. Every time I try to set it to checked, it tells me the property is read only and I cannot change it.
 
Dim dgi As DataGridItem

For Each dgi In datagrid1.Items
dim cb as new CheckBox
cb = findcontrol (dgItem.FindControl("chkExportItemSelected"))
cb.Checked = TRUE

Next
 
Thank you very much. Obviously I did not understand what you were trying to tell me. I thought I was creating a new control and did not connect that I was creating a way to reference the existing control.

Again, Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top