Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 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
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");
                }
            }
        }
Posted
Comments
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
Sergey Alexandrovich Kryukov 29-May-13 9:32am    
If you already have check boxes, why yes/no? Do you understand the purpose of a check box?
—SA
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

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



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