Solved

Programmatically create Check box DataGridView with INT fields

Posted on 2011-03-14
8
898 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 50 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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 450 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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