Click to See Complete Forum and Search --> : datagrid checkboxen validation


father_pedro
January 3rd, 2006, 04:39 AM
Ok, what i'm looking for will sounds a bit weird I know ...

I have a Datagrid bound to an ArrayList of custom class objects.
Those objects contain 3 Bool parameters, which can be adjusted in the datagrid by using checkboxcolumnstyles. so, what i want is that if one of the tree booleans (checkboxes) is set to true, that the others become disabled of something like that

Found this solution: In the Set part of the parameter:

if(Bool1==false && Bool2==false)
{Bool3 = value;}

This has the effect that when you check one of the checkboxs, where another one has already been checked that the datagrid unchecks it.

"Is this the best practice ??" or is there a better solution ??

What I'm missing is the possibility to show an errormessage when this occurs.

Was thinking about this: Is it possible to throw an exception in the else part of the if above and where should i be able to catch it ??

Thanks in advance

jhammer
January 3rd, 2006, 08:21 AM
If I were you, the code of the object would look like this:

public class MyObject
{
private bool _bool1 = false, _bool2 = false, _bool3 = false;
public bool Bool1
{
get { return _bool1; }
set
{
_bool2 = !value;
_bool3 = !value;
_bool1 = value;
}
}
public bool Bool2
{
get { return _bool2; }
set
{
_bool1 = !value;
_bool3 = !value;
_bool2 = value;
}
}
public bool Bool3
{
get { return _bool3; }
set
{
_bool1 = !value;
_bool2 = !value;
_bool3 = value;
}
}
}

One you bind an ArrayList of this objects to a grid, it should have the effect you were looking for.

father_pedro
January 3rd, 2006, 10:27 AM
Indead, thx, but is there a possibilty so send a errormessage of some kind ?

torrud
January 3rd, 2006, 10:55 AM
You can easily expand the set operator:

public bool Bool3
{
get { return _bool3; }
set
{
if(_bool1||_bool2)
throw new Exception("Another checkbox is selected at the moment!");

_bool1 = !value;
_bool2 = !value;
_bool3 = value;
}
}

father_pedro
January 3rd, 2006, 11:09 AM
yes, getting to the point where i'm stuck :p


Where to I catch that exception :(

torrud
January 3rd, 2006, 12:05 PM
I would prefer to catch the exception in the presentation layer, because so you have the possibility to show up an information form to the user with more detailed information.