Solved

Programmatically create Check box DataGridView with INT fields

Posted on 2011-03-14
8
895 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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