all groups > asp.net datagrid control > september 2005 >
You're in the

asp.net datagrid control

group:

Disable checkbox when field value = null


Disable checkbox when field value = null SRuby
9/26/2005 9:22:04 PM
asp.net datagrid control:

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

Re: Disable checkbox when field value = null Darren Kopp
9/27/2005 1:16:54 PM
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
[quoted text, click to view]
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

[quoted text, click to view]

AddThis Social Bookmark Button