Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# DataGridView Checkbox Value. Add 1 to current value whenever the checkbox checked value is true

$
0
0

Good Day,

I would like to ask for help, I am new to C#

I am currently working on an attendance monitoring project where I have a dataGridView with checkbox column.

What I wanted to happen is whenever I click a button, the value of it (which is 1 or 0) will be added to the current value of the checkbox. Initially the value of each column is zero, whenever the student's attendance for that day is checked which means the student is present and add 1 to the database, else add 0, which means the student is absent. So that at the end of the month or a certain period, the sum will be the total days that the student is present. 

this is how I added my DataGridView checkbox column:

DataGridViewCheckBoxColumn day1 = new DataGridViewCheckBoxColumn();
            day1.HeaderText = "Attendance";// +DateTime.Now.ToShortDateString();
            day1.Name = "day1";
            day1.Width = 150;
            day1.ReadOnly = false;
            day1.FillWeight = 10;
            day1.TrueValue = 1;
            day1.FalseValue = 0;
            DGV_Attendance.Columns.Add(day1);
and this is the button click event that I was able to come up to:
  private void btnSaveAttendance_Click(object sender, EventArgs e)
            {
                string myConnection = "datasource=localhost;port=3306;username=root;password=123";
                MySqlConnection conn = new MySqlConnection(myConnection);

                foreach (DataGridViewRow row in DGV_Attendance.Rows)
                {

                  using (MySqlCommand cmd = new MySqlCommand("update pcdcsmis.attendance set date= @date + 1", conn))
                               {
                               cmd.Parameters.AddWithValue("@date",  row.Cells["day1"].Value);
                                conn.Open();
                                cmd.ExecuteNonQuery();
                                conn.Close();

                                }

                 }
            }

But this code doesn't work, whenever I check only one checkbox the value turns to null, and when I click the checkbox of the last or second to the last checkboxes, all of the rows even those that were unchecked adds 1.

Is this even possible or do I have to find another approach to the problem?

Looking forward to your help,

Pardon any misspelled words or wrong grammar,

Thanks and Best Regards!




Geee



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>