Click here to Skip to main content
13,092,862 members (97,351 online)
Rate this:
Please Sign up or sign in to vote.
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
private void button1_Click(object sender, EventArgs e)
            int no_of_branch=0;
            int batch_count;
            int totalbatch = 0;
            foreach (var checkBoxSele in grpBox1.Controls.OfType<CheckBox>().OrderBy(m => m.Name))
            string[] str = new string[no_of_branch];
            foreach (var checkBoxSele in grpBox1.Controls.OfType<CheckBox>().OrderBy(m => m.Name))
                if (checkBoxSele.Checked)
                    str[no_of_branch] = checkBoxSele.Text;
            for (int j = 0; j < no_of_branch; j++)
                    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");
                            case "Eve": vtime = DateTime.Parse("16:00");
                        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.Columns[0].ReadOnly = true;
                    dataGridView1.Columns[1].ReadOnly = true;
                    DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
                    chk.HeaderText = "Freeze";
                    chk.Name = "chk";
                    label3.Text = totalbatch.ToString();
                catch (OleDbException ex)
                    MessageBox.Show("Please close the Databse");
Posted 29-May-13 3:21am
CHill60 29-May-13 9:29am
So what is your problem?
vishal deb 29-May-13 9:41am
I want to show checkbox under each day(columns) and values from database in these checkbox.
CHill60 29-May-13 9:55am
Well you're already adding rows to the dataGridView so just set the appropriate values for the checkboxes in the same line. I don't know your database schema and you've used Select * from so I can't help with which (database) columns should be used for what. It also rather depends on *how* you're storing the yes/no stuff on the database ... i.e. can you cast ds3.Tables[0].Rows[k][x] to a boolean or do you get the words "Yes", "No" returned
If you already have check boxes, why yes/no? Do you understand the purpose of a check box?
vishal deb 29-May-13 9:38am
Yes I do understand the purpose of checkbox whether the option is selected or not. So if I want to select Sun day then I I want to put a tick on checkbox means Select Sunday and if it is not tiked means I don't want Sunday. And the value of check box will be saved in Access database as Yes/No. Well what best way you have please let me n
Than you should understand that a singe check box is already a yes/no selection. So what's the problem? Just use the DataGridViewCheckBoxColumn.

Marc A. Brown 29-May-13 15:09pm
So, do you have a separate column in your data table for each day of the week (columns for Sunday, Monday, Tuesday...Friday with a true/false or similar value for each) or is it a single column specifying the day (DayOfWeek, containing "Sunday" or "Monday" or...or "Friday")? Please update your question with this information (click the "Improve question" link).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100