Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 DataGridView
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)
        {
            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 29-May-13 4:21am
Comments
CHill60 at 29-May-13 9:29am
   
So what is your problem?
vishal deb at 29-May-13 9:41am
   
I want to show checkbox under each day(columns) and values from database in these checkbox.
CHill60 at 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 at 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 at 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
Sergey Alexandrovich Kryukov at 29-May-13 9:42am
   
Than you should understand that a singe check box is already a yes/no selection. So what's the problem? Just use the DataGridViewCheckBoxColumn.
 
—SA
Marc A. Brown at 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
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2014
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