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!

Checkboxes in Datagrids

Status
Not open for further replies.

chops03

Programmer
Mar 17, 2003
12
US
I'm having some trouble with a checkbox in a datagrid. I'm using VB.net for the behind code. When the code is run after a button click, the checked property is not being set to true although the box is checked. Here's the code to access the checkbox:

Dim cb As CheckBox
Dim dgi As DataGridItem
Dim i_Counter As Int16

For Each dgi In dg_Orders.Items
cb = CType(dgi.Cells(0).Controls(1), checkbox)
If cb.Checked = True Then
'Do something
End If
i_Counter += 1
Next

I can set the checkbox equal to checked with cb.checked = true, but for some reason when the box is clicked, the checked is not coming back true. Any ideas are greatly appreciated as I'm running out of them.

Thanks
 
Are you sure that your code is finding the correct control?

Not sure how this translates to vb.net, but in c# I would probably do something like this:

foreach(DataGridItem dgi in dg_Orders.Items)
{
if(dgi.FindControl("[ControlName]") != null)
{
if(((CheckBox)dgi.FindControl("[ControlName]").Checked)
{
// Do something here
}
}
}

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Also, you need to make sure that you're running that code AFTER page_load. The values are not available in Page_Load because the LoadViewState for the DataGrid has not yet been fired.

Your values will be available during your normal event handlers for your various page objects, and also will be available in Page_PreRender.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
It seems to be finding the right controls. For some reason the mouse click is not setting checked = true. If I use the following code:

Ctype(dgi.FindControl("chk_completed").Checked = True
If CType(dgi.FindControl("chk_completed"), CheckBox).Checked = True Then
Label1.Text = "Works"
End If

The label will display the text, but if I don't manually set the checked property to true, it does not work.

I believe the code is being executed after page_load. The page loads, user clicks checkboxes and then clicks button to update and the code executes in button_click.
 
the true false is actually -1 for true and 0 for false

For Each dgi In dg_Orders.Items
cb = CType(dgi.Cells(0).Controls(1), checkbox)
Dim Active As Integer = cb.Checked
If Active = -1 Then
'Do something
End If
i_Counter += 1
Next


I dont remember why i ended up doing it this way, but it works. Try that out.
 
It looks like the problem was with the datagrid EnableViewState property. I set it to false and on initial testing, the checkboxes are working correctly. One of these days I'll fully understand .Net. Thank you all for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top