Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Programmatically create Check box DataGridView with INT fields

Posted on 2011-03-14
8
Medium Priority
?
901 Views
Last Modified: 2013-12-17
Hey all!

Just a small problem at the moment which I need solving if anyone can help.

I would like to programmatically make all int fields within a DataGridView check boxes. Does anyone know how? I use the following to fill the DataGrid


 
DataTable DT_Courses = new DataTable();

            DT_Courses = KazooSoft.MSSQL_Functions.DBQuery(
                "SELECT     students.student_cn, students.student_fn, students.student_ln, course.course_title, lm_statements.archived, lm_statements.printed " +
                "FROM course INNER JOIN " +
                      "lm_statements ON course.course_code = lm_statements.course_id INNER JOIN " +
                      "students ON lm_statements.student_id = students.person_code " +
                "WHERE course.course_code = '"+ combo_Courses.SelectedValue.ToString() +"'",
            KazooReports.Properties.Settings.Default.MSSQL_username,
            KazooReports.Properties.Settings.Default.MSSQL_password,
            KazooReports.Properties.Settings.Default.MSSQL_database,
            KazooReports.Properties.Settings.Default.MSSQL_server
            );

            dataGridView_PrintArchive.DataSource = DT_Courses;

Open in new window


But they just show up as blank fields with either a 1 or null.

Cheers,
0
Comment
Question by:KazooSoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:Nash2334
ID: 35131927
If you set the DGV to Autogenerate columns, it's going to output all of them as text strings.  The easiest thing to do here would be to bind the DGV to the datasource in the VS Designer and then change the column types there.
0
 
LVL 10

Assisted Solution

by:Nash2334
Nash2334 earned 200 total points
ID: 35132749
You can also check datatypes in the datatable and add columns programmatically to the DGV after disabling autogeneratecolumns.

            // Create datatable
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Int1", typeof(System.Int32)));
            dt.Columns.Add(new DataColumn("Int2", typeof(System.Int32)));
            dt.Columns.Add(new DataColumn("String1", typeof(System.String)));
            dt.Columns.Add(new DataColumn("Int3", typeof(System.Int32)));

            dt.BeginLoadData();
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = 0;
            dr[2] = "Hello world.";
            dr[3] = 1;

            dt.Rows.Add(dr);
            dt.EndLoadData();

            // Add columns to datagridview
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.DataType == typeof(System.String))
                    dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
                else if (dc.DataType == typeof(System.Int32))
                    dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn());
            }

            dataGridView1.DataSource = dt;
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 35133349
when you select, select as boolean

ie

"SELECT  cast(case when students.student_cn == 0 then 0 else 1 end as bit) as students.student_cn,  students.student_fn, students.student_ln, course.course_title,
...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:KazooSoft
ID: 35146273
EDDYKT: That causes a SQL error

 
"SELECT     students.student_cn, students.student_fn, students.student_ln, course.course_title, lm_statements.archived, cast(case when lm_statements.printed == 0 then 0 else 1 end as bit) as lm_statements.printed " +
                "FROM course INNER JOIN " +
                      "lm_statements ON course.course_code = lm_statements.course_id INNER JOIN " +
                      "students ON lm_statements.student_id = students.person_code " +
                "WHERE course.course_code = '"+ combo_Courses.SelectedValue.ToString() +"'",

Open in new window


Nash2334: I know this is a solution but not my ideal solution.

0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 35146454
should be just single =

ie
cast(case when lm_statements.printed = 0 then 0 else 1 end as bit) as lm_statements.printed
0
 
LVL 4

Author Comment

by:KazooSoft
ID: 35146524
That kind of works but. The values in the database are either null, 0, 1. How will I get it to check for example if printer = 0 or null else 1 end?
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1800 total points
ID: 35146545
cast(case when lm_statements.printed is null or  lm_statements.printed = 0 then 0 else 1 end as bit) as lm_statements.printed
0
 
LVL 4

Author Comment

by:KazooSoft
ID: 35146558
Thank you so much.

If i use

cast(case when lm_statements.printed is null or lm_statements.printed = 0 then 0 else 1 end as bit) as printed

Then it also seems to make the Archive field a bool too!? Strange but great thank you!
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question