Disable checkbox when field value = null

S

SRuby

Hi,

I try to disable /hide datagrid checkbox when the value of the field is
null,

I have try to put some code in ItemDataBound to invisible the checkbox
when databind() fired

Here's the codein ItemDataBound event

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.EditItem Or e.Item.ItemType =
ListItemType.SelectedItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
If drv("isexist").ToString = "" Then
e.Item.Cells(2).Visible = False
end if
end if

When I debug the program, the ItemDataBound was triggered once when the
ListItemType = Header, after that, the error raises
Cast from type 'DBNull' to type 'Boolean' is not valid.

Why the OnItemDataBound only triggered once?

thank

Ruby
 
D

Darren Kopp

The ItemDataBound is firing once because nothing is being bound in your
Header so it just fires without problem. That's why you are getting your
error on the first item in the datagrid that is actually data bound.

I had a similar issue that I had to address with a datagrid with several
items that needed to be inaccessible to the user, so what I did was I wrote
a small function that I put in my code behind like the following.

protected function IsNullValue(val as Object) as Boolean
if val.Value = DBNull.Value then
return false
else
return true
end if
end function

Sorry if the function is off a bit, I'm a bit rusty on my VB.Net. Anyway
what I did then was on the actual checkbox control, I bound the Enabled
value to
IsNullValue(DataBinder.Eval(Container.DataItem, "yourcolumn")

If the value passed into the function is a null value, then it returns
false, disabling the checkbox control, if there is a value, then the
checkbox is enabled. You could also do so for the checked property.

Hope this gets you started,
Darren Kopp
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top