By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Getting the Value of a Checkbox in the GridView Selected Row

P: n/a
I have a GridView that displays several columns, one of which is a
checkbox field. This is populated at runtime from a SqlDataSource and
is not enabled (the user can't change it's value). I have turned on
the "Enable Selection" option, and I know I can identify what is the
currently selected row.

How can I get the value [True/False] of the checkbox in the selected
row? For columns that are just text, this works:
System.Diagnostics.Trace.WriteLine(Me.GridView1.Se lectedRow.Cells(3).Text)
...but it does not work on a column that is a checkbox (returns
nothing
since there is no text in that column). I have tried to use the
Ctype
method to cast the column as a checkbox, but I can't get that to work
either.
I realize that I am missing something stupid, and I would appreciate
a
tip or suggestion.
Thank you!

Mar 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
check out this code sample at ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...=GridViewCheck

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
"Kirk" <lo****@hotmail.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
I have a GridView that displays several columns, one of which is a
checkbox field. This is populated at runtime from a SqlDataSource and
is not enabled (the user can't change it's value). I have turned on
the "Enable Selection" option, and I know I can identify what is the
currently selected row.

How can I get the value [True/False] of the checkbox in the selected
row? For columns that are just text, this works:
System.Diagnostics.Trace.WriteLine(Me.GridView1.Se lectedRow.Cells(3).Text)
..but it does not work on a column that is a checkbox (returns
nothing
since there is no text in that column). I have tried to use the
Ctype
method to cast the column as a checkbox, but I can't get that to work
either.
I realize that I am missing something stupid, and I would appreciate
a
tip or suggestion.
Thank you!

Mar 6 '07 #2

P: n/a
David,

Thanks for your quick reply! Unfortunately, I must not be using your
example correctly.

I modified the code to try and use it like this:

Dim cb As CheckBox =
CType(GridView1.SelectedRow.FindControl("Working") , CheckBox)
If Not cb Is Nothing Then
If cb.Checked Then
System.Diagnostics.Trace.WriteLine("Checked!")
End If

Else
System.Diagnostics.Trace.WriteLine("NOT Checked")
End If

The "cb" [checkbox] is always set to "Nothing" - that is, it never
finds the control called "Working". I have double-checked my spelling
for the column name in the GridView and tried another checkbox column,
and I got the same result.

Can you see where I am going wrong? Thank you again.

Mar 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.