Simple Question - I Think:/

  • Thread starter Thread starter MadCrazyNewbie
  • Start date Start date
M

MadCrazyNewbie

Hey Group,

A Really Simple one, I have a feild in my Database which I would like a YES
or a NO on, how would i setup my ComboBox to allow this, and for it to
update my DB?

Many Thanks
MCN
 
Make your database field as bit. this maps well to System.Data.SqlDbType.Bit which underlying is System.Boolean

Regards

Deepa
[I Code, therefore I am

----- MadCrazyNewbie wrote: ----

Hey Group

A Really Simple one, I have a feild in my Database which I would like a YE
or a NO on, how would i setup my ComboBox to allow this, and for it t
update my DB

Many Thank
MC
 
Hi Mad,

It seems maybe simple for you however not for me.

A combobox can hold lines not only Yes or No.
For that is one checkbox more than enough,

Maybe you can explain a little bit more?

Cor
 
MadCrasyNewbie:

Like Deepak mentioned, you can set the field as a bit and therefore make it
an either/or scenario. If you bind it to a grid, then it will automatically
come in as a CheckBox for instance. 0 and 1 map to True and false in text
so databinding it to that field will get you the result. Remember though
that binding to this field is going to give you a SelectedValue or True
and/or False not 0 or 1. Moreover, if you check the type, you'll get
System.Boolean. When I did this a while back, I set the ValueMember to the
Bit field and I expected the Display to be true or false but the Actual
value to be 0 or 1. That's not the case, it's true or false for both.

This will still update like you expect though, so it's not really a problem
although I thought it might be when I first discovered it.

The bottom line, Deepak provided a complete and correct answer. When I was
learning this, I often thought too hard about stuff worrying about things
that didn't matter. In fact it all makes perfect sense now but I figured
I'd mention it in case anyone else suffers from the same problem ;-)

However, for a combo box and true and false,
 
Hi Bill,

Now you completly get it, what has it to do with a combobox, because that
was the question.

Something as How do I put "No or Yes" in a combobox related to the dataset.

Let the sun also shine for me?

Cor
 
Hey Cor:

The Bit Field maps back to System.Bool which can only evaluate to true or
false. Since it maps back, it converts it from 0 to 1 to true or false.
Going the other direction, it turns true or false into 0 or 1. If the DB
field is Bit, then this happens automatically when you bind to the field.
Originally I was expecting the ValueMember to be 0 or 1 and the
DisplayMember to be True or False. However, in the overall context this
wouldn't make sense b/c they map back to bool. If it didn't, then True and
False couldn't appear automatically. Since they do appear, the mapping is
transparent. So not only does it happen without you doing anything, the
only value it has is True or False. However, when you take a System.Bool of
True or False and send it back to a DB with a bit field, then the literal
"True" or "False" can't be used b/c there's not enough room allocated and
until Yukon, the DB has no idea what a language's types are supposed to
represent. Anyway, when you send back a System.Bool, it gets converted into
bit (once again, and this is the nice part) automatically.

HTH,

Bill
 
Hi Bill,

I did not ask what is a bit, a boolean or a byte and also not the
representing in a SQL database, that I know a for a long time.

I asked what has that to do with a combobox.

Cor
 
It's hard to explain the binding example in this context without it. When
you use a Bit field, it's translated to bool. When you bind to it, it's
respective values are ghoing to be the literal True or False not 0 or 1
which is what I'd have expected the ValueMember to be.

Anyway, say I have a query that has a field "Exempt" and that's a bit. To
bind it to a ComboBox and have it appear as true or false all I need to do
is this assuming I have my datatable filled already:

comboBox1.DataSource = dt;

comboBox1.ValueMember = "Exempt";

Now, with no other code, I just made the values in that combobox either
"True" or "False" If I bind to a grid with the same dataset, then every
time I switch rows in the grid, my combobox will show the respective value
either True for exempt or False if they aren't. No Items need added or
anything else.

But the reason I mentioned bit was that if the translation didn't occur,
you'd expect that you'd have to bind in a different way.. with an expression
or something to switch to True if you had a 1 or false if you had a 0. It's
cool yet suprising that all of the translation, in both directions is done
for you.

Sorry if I didn't answer your question before, but hopefully this answers
it. If not, let me konw.

Bill
 
Hi Bill,

No because I anserwered this question with something as: Why do you not use
a checkbox, that is for Boolean operators, I think that is more proper than
using a combobox. You only have to set the text Yes (in your country) with
it and all is clear.

In a datagrid the boolcolumn (checkbox) is even standard.

Cor
 
I agree but I was addressing his question about how to do it with a
ComboBox, and I was just answering on how to do it. I didn't negate your
point at all or imply you were wrong or anything. I simply discussed using
a combobox if he decides that's what he wants.

To be honest, I wouldn't use either, I'd opt for Radio buttons becuase I
think they look better in the UI. Usually I'd opt for a checkbox when I
only have two options, but for Male/Female, Yes/No and True/False, I think
radio buttons are more clear make more sense. In the case of true/ false, I
think showing false in the affirmative as opposed to showing it as the
absence of True is more clear.

But it's just a UI design issue and personal taste. I'm hesitant to comment
on what people want for their UI, it's their app and that's really a matter
of taste.

Incidentally, Deepak made the same observation.
 
Back
Top