Click here to Skip to main content
15,886,806 members

How to show yes/no value in DataGridViewCheckBoxColumn

vishal deb asked:

Open original thread
Dear All
I have 7 columns of yes/no data type I want to show them in datagridview in checkbox form. I have written this code where I am showing some values from database to datagridview with a column having check box. I just want to show databse value in Under Sun,Mon,Tue,Wed,Thr,Fri,Sat columns Please refer the code
C#
private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            int no_of_branch=0;
            int batch_count;
            int totalbatch = 0;
            foreach (var checkBoxSele in grpBox1.Controls.OfType<CheckBox>().OrderBy(m => m.Name))
            {
                no_of_branch++;
            }
            string[] str = new string[no_of_branch];
            no_of_branch=0;
            foreach (var checkBoxSele in grpBox1.Controls.OfType<CheckBox>().OrderBy(m => m.Name))
            {
                if (checkBoxSele.Checked)
                {
                    str[no_of_branch] = checkBoxSele.Text;
                    MessageBox.Show(str[no_of_branch]);
                    no_of_branch++;
                }
            }
            for (int j = 0; j < no_of_branch; j++)
            {
                try
                {
                    aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str[j] + "'", main_connection);
                    aAdapter3 = new OleDbDataAdapter(aCommand3);
                    ds3 = new DataSet();
                    aAdapter3.Fill(ds3, "app_info");
                    batch_count = ds3.Tables[0].Rows.Count;
                    
                    dataGridView1.ColumnCount = 11;
                    dataGridView1.Columns[0].Name = "Batch Code";
                    dataGridView1.Columns[1].Name = "Centre";
                    dataGridView1.Columns[2].Name = "Duration";
                    dataGridView1.Columns[3].Name = "Time";
                    dataGridView1.Columns[4].Name = "Sun";
                    dataGridView1.Columns[5].Name = "Mon";
                    dataGridView1.Columns[6].Name = "Tue";
                    dataGridView1.Columns[7].Name = "Wed";
                    dataGridView1.Columns[8].Name = "Thr";
                    dataGridView1.Columns[9].Name = "Fri";
                    dataGridView1.Columns[10].Name = "Sat";
                    for (int k = 0; k < batch_count; k++)
                    {
                        string exisBatch = ds3.Tables[0].Rows[k][0].ToString();
                        int batlen = exisBatch.Length;
                        string dur = ds3.Tables[0].Rows[k][16].ToString();
                        DateTime vtime = DateTime.Parse("16:00");
                        switch (dur)
                        {
                            case "Mor": vtime = DateTime.Parse("08:00");
                                break;
                            case "Eve": vtime = DateTime.Parse("16:00");
                                break;
                        }
                        shortBCode = ds3.Tables[0].Rows[k][1].ToString() + ds3.Tables[0].Rows[k][15].ToString() + ds3.Tables[0].Rows[k][16].ToString().Substring(0, 1) + exisBatch.Substring(batlen - 3);
                        dataGridView1.Rows.Add(shortBCode,
                        ds3.Tables[0].Rows[k][2].ToString(),
                        ds3.Tables[0].Rows[k][10].ToString(),
                        vtime.ToString("HH:mm"),
                        ds3.Tables[0].Rows[k][7].ToString());
                        
                        //comboBox2.Items.Add(ds3.Tables[0].Rows[i][0].ToString());
                        totalbatch++;
                    }
                    dataGridView1.Columns[0].ReadOnly = true;
                    dataGridView1.Columns[1].ReadOnly = true;
                    DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
                    dataGridView1.Columns.Add(chk);
                    chk.HeaderText = "Freeze";
                    chk.Name = "chk";
                    label3.Text = totalbatch.ToString();
                }
                catch (OleDbException ex)
                {
                    MessageBox.Show("Please close the Databse");
                }
            }
        }
Tags: C# (C# 3.0), DataGridView

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900