Solved

Programmatically create Check box DataGridView with INT fields

Posted on 2011-03-14
8
892 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now