datagridcontrol >> Disable checkbox when field value = null

by SRuby » Tue, 27 Sep 2005 12:22:04 GMT



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

*** Sent via Developersdex http://www.developersdex.com ***

datagridcontrol >> Disable checkbox when field value = null

by Darren Kopp » Wed, 28 Sep 2005 03:16:54 GMT


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

Similar Threads

1. Disabling Databound Checkbox if its value in table is null

Hi,

I have created a table which represents a simple Questionaire. The
Questions and various options (answers) form columns and there are
additional bit columns which represent whether the respective option is
selected or deselected. One problem I am facing is that my questions
can have varied number of options like some may have 4 options, some
may have 5, and others may have 6.

The form is bound to the table, options are represented by lables and
bit columns by checkboxes. The form display all records from the
Questionaire at a time. Now I do not want to display the checkboxes and
respective Option when their value is null in the table. Is this
possible? If yes How?

Thanks & Regards,
Mandar

2. Allow Data Entry if Field is Null, Disable if Field is not Null

3. Disable multiple fields based on lookup value of seperate field

I have a transactions detail form for entering financial transactions in a 
transaction detail table.  One of the fields in this form is an account 
lookup field to specify which account to charge the transaction to.  I would 
like to disable three other fields when the account field is set to the 
mileage account.  Can anyone help?

4. disable a field on form if a value is in another field

5. Enable/Disable a field based value of another field in the same ta

"Deecrypt" wrote:

> Hi all,
> Access noob here.  I have two fields (availability, borrower) in the
> table dvd.  The availability field has a lookup as the data type so
> user can select either "Borrowed" or "Available" vaules.  Is it
> possible to make the "borrower" field uneditable if the value in the
> "availability" field is "Available" and editable if "Borrowed" is
> selected?
> 
> Thank you in advance
> Deecrypt
> 
> 
You can do this but more information is required first. You say fields, 
these are table items, does this mean you are working directly in the table 
and not in a form? If so, dont, its very bad practice.

Secondly you say that the availability field is a lookup, again, this could 
mean you are working directly in tables, again don't, and certainly don't use 
the lookup option in the table design as this causes numerous problems in 
Access.

The right way forward is to design a simple form based either on your table 
or on a query based on your table. Your should then have "Controls" which 
appear on the form not fields.

Once you have done that, it is possible to put code in the on change event 
of the one control which limits edits in the other control.

This can be advised once you state the controls that appear on your form. 
Bear in mind that you can still use Lookups in forms. I hope I have not 
mis-understood your question?????

HTH Mike B
-- 
An Engineers Prayer:
At the very end of the day,
when all else fails,
you have tried all,
you have shouted at and blamed the innocent,
and asked everyone you know,

READ THE INSTRUCTION MANUAL.

6. Enable/Disable a field based value of another field in the same table - Microsoft Office Access

7. Enable/Disable a field based value of another field in the sam

Khurram:

What you now need is a third table to represent the many-to-many 
relationship between DVD and Borrower; lets call it Borrowings for the moment 
but you can call it whatever name you wish of course. This table will have 
two foreign key columns, dvd_no and borrower_no which reference the primary 
keys of DVDS and Borrower in separate many-to-one relationships.    The table 
will also have columns for other attributes of each 'borrowing' such as 
date_borrowed and possibly date_returned.  You might want other columns to 
record other facts about each 'borrowing'.

For data entry the usual approach would be to have a DVD form based on a 
query such as:

SELECT *
FROM DVD
ORDER BY dvd_name;

which would order the form by the DVD name, and embedded within it a subform 
in continuous form view based on a query such as:

SELECT *
FROM Borrowings
ORDER BY date_borrowed;

The LinkMasterFields and LinkChildFields properties of the subform control 
would be dvd_no.  In the subform you'd have a combo box bound to the 
borrower_no field with a RowSource of:

SELECT borrower_no, 
(borrower_first_name + " ") & borrower_last_name
AS full_name
FROM Borrower
ORDER BY borrower_last_name, borrower_first_name;

Its properties would be as follows:

BoundColumn	1
ColumnCount	2
ColumnWidths	0cm;8cm

For the last you can of course use the rough equivalent in inches, but the 
first dimension must be zero to hide the first column.

You don't need any control bound to the dvd_no on the subform, but you would 
add controls for date-borrowed, date-returned etc. Whenever a dvd is borrowed 
you simply go to its record in the main form and add a row in the subform, 
selecting the borrower from the combo box's list and entering the date 
borrowed.

You can also reverse the set-up of course having a form based on the 
Borrower table and a subform on the Borrowings table but this time with the 
combo box bound to the dvd_no field and with its RowSource based on the DVD 
table.  This form would show you all the DVD's borrowed by each Borrower.

Ken Sheridan
Stafford, England

"Deecrypt" wrote:

> Hi all,
> As agreed, I now have forms to enter data in my Dvd and borrower
> tables.  The aim of this database is to be able to track who has
> borrowed a particular dvd.  Currently I have the two tables with their
> respective forms for data entry.  Each table has a unique id assigned
> as an autonumber.  No relationships currently exist.  I need to know
> how I can proceed save borrowed dvd information in this database.  No
> need to be detailed.  I just need to know the basic concept and I'll
> find the nitty gritty details.  I have listed the field names of the
> two tables below
> 
> dvd:
> 
> dvd_no                       Autonumber
> dvd_name                  Text
> region                        Text
> 
> 
> borrower:
> 
> borrower_no                AutoNumber
> borrower_last_name    Text
> borrower_first_name    Text
> phone                        Text
> 
> 
> Thank you kindly
> Khurram
> 
> 

8. FormView checkbox two-way bind problem with null fields - ASP.NET Web Controls