This is a migrated thread and some comments may be shown as answers.

Boolean column in GridView and DB

10 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
shahar
Top achievements
Rank 1
shahar asked on 25 Mar 2011, 04:57 PM
Hi,

One of my DB column is a char(1). I use it to report "y" or "n" for a row.
I've added GridView which has a combobox column.

I used dataset to connect between the two.
How to I change the "true" of "false" which the combobox returns to a "y" or "n" value?

Now when I change the value of "Active" combobox (see image attached), an exception thrown "Cannot set column 'bool_active'. The value violates the MaxLength limit of this column."

10 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Mar 2011, 05:41 PM
Hello Shahar,

This happens because the type of your column is char(1), why don't you use bit datatype for this?

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Richard Slade
Top achievements
Rank 2
answered on 26 Mar 2011, 06:23 PM
Hello,

In addition to Emanuel's comments, you might also be interested in using a TypeConverter for this scenario.
Please visit this help topic for more information

Hope that helps
Richard
0
shahar
Top achievements
Rank 1
answered on 26 Mar 2011, 06:27 PM
Hello Emanuel ,

What do you mean bit data type?
My database column is indeed varchar(1), and I want it to receive 'y' or 'n' only.
I need the combobox to be checked if column is 'y' and unchecked if value retrieved is 'n'.

I didn't understand your answer. How do I implement it?
0
Richard Slade
Top achievements
Rank 2
answered on 26 Mar 2011, 06:32 PM
Hello,

There are two ways to do this. The more obvious route is to convert your Y/N to a Bit as it comes out of your database so that the checkbox column renders correctly. In fact, I would advise changing your database column to a bit column and only having Y/N on the UI side. Storing Y/N in your database is slower to index and more cumbersome for these types of scenarios.

The other way would be to use a Type COnverter as I mentioned above.
Regards,
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Mar 2011, 07:36 PM
Hello again,

Please think about something else, databases should be constructed in such a way so that data can be accessed as fast as possible. So because of this, if you would need to just get all the records that are true or all the records that are false, a query using a bit data type or a boolean in c# or vb will always be much faster than a string / char comparison. If you would just change that type in your table you will see that everything will work. But you won't have Y / N you will have 1/0 or true / false.

In my point of view this is the way to go, but if you really really want to go with Y / N i will provide you with a solution to do just that.

Please let me know what you decide.

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
shahar
Top achievements
Rank 1
answered on 28 Mar 2011, 09:05 PM
The fact is that I can't change the databases' types, therefore I have to compare the combobox "status" to y/n.
But, how cant I convert the value retrieved/given from/to the db?

Anyway can you provide me a suggestion for a solution?

Many thanks!!!
0
Richard Slade
Top achievements
Rank 2
answered on 29 Mar 2011, 09:55 AM
Hello,

Another option, other than those that Emanuel and I have already suggested is to convert the value to a bit value as it comes out of the database in your SQL Stored Procedure or SQL Command text.

Hope that helps
Richard
0
Svett
Telerik team
answered on 30 Mar 2011, 03:40 PM
Hello Shahar,

In addition to Richard's and Emanuel's answers, you can read this documentation article that illustrates how you can make your data source compatible with a concrete column (in your case GridViewCheckBoxColumn). In addition, you can take a look at our Examples application, Convert Values section.

Greetings,
Svett
the Telerik team

0
shahar
Top achievements
Rank 1
answered on 31 Mar 2011, 09:44 PM
Hi,

I have succeed! I've used the information about Converting Data Types you've attached.
I've made a new class ToggleStateConverter and added to the checkbox the command
            gridViewCheckBoxColumn1.DataTypeConverter = new ToggleStateConverter();

BUT,
I've noticed to a new problem!
Everytime I go back to the Main Form Design, the above line is just removed automatically! and the properties for gridViewCheckBoxColumn1 comes back to default.
Look at the attached files. the first, before.jpg, was taken before touching "frmMain.cs [Design]", and after.jpg after clicking on it, and returning back to the designer cs file.

I believe it happens due to an automatic refresh coming with the radGridView.
How do I fix it???

0
Svett
Telerik team
answered on 04 Apr 2011, 04:00 PM
Hi Shahar,

You should not set the DataTypeConverter property of the column at design time. Instead, you should set it at run time. You can do that in the constructor of the form or in the Load event of form.

Greetings,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
shahar
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
shahar
Top achievements
Rank 1
Svett
Telerik team
Share this question
or