Solved

Programmatically create Check box DataGridView with INT fields

Posted on 2011-03-14
8
896 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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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