Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I need some help. I searched mostly sites but did not find desired code for my query.
I am coding for a module of "Attendance of Class" in window application and here I have a gridview having student name(textbox column), roll no(textbox column), present(checkbox column), absent(checkbox column), on leave(checkbox column).
1) I want only one checkbox should be checked in a row.
2) I want to show the total present student(means total of "present checkbox" column checked checkboxes) in a label names total present students.

Here is my "Save Button" click event to save the values into the table.
C#
private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("Select Date FROM tbAttendance WHERE Date='"+dtpDate.Value+"'", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("Attendance Already Added, Please Select another Date for this Class and Section");
                }
                else
                {
                    if (cbClass.SelectedIndex != 0 && cbSection.SelectedIndex != 0)
                    {
                        con.Open();
                        for (int i = 0; i < dgvAttendance.Rows.Count - 1; i++)
                        {
                                SqlCommand cmd = new SqlCommand("Insert into tbAttendance VALUES('" + dgvAttendance.Rows[i].Cells["Student_id"].Value + "', '" + dtpDate.Value + "', '" + dgvAttendance.Rows[i].Cells["Present"].Value + "', '" + dgvAttendance.Rows[i].Cells["Absent"].Value + "', '" + dgvAttendance.Rows[i].Cells["Leave"].Value + "', '" + dgvAttendance.Rows[i].Cells["Remarks"].Value + "')", con);
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Successfully submited");
                        }
                        con.Close();
                    }
                    else
                    {
                        MessageBox.Show("Please Select Class and Section");
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }


Any Suggestion would be appreciated. Thanks in advance
Posted

1 solution

Are you aware the WinForms CheckBox can, optionally, have three 'CheckState values ?

CheckState.Checked
CheckState.UnChecked
CheckState.Indeterminate

To use the CheckBox with three states, set the 'ThreeState Property to 'true. So, you could handle "present," "absent," and "on leave" with one Control, not three.

Second, you can use RadioButtons, rather than CheckBoxes, to model mutually exclusive states, which is the way your application uses CheckBoxes now. Why write the code to make sure only one CheckBox is checked, when using RadioButtons will give that to you "for free" ?

"2) I want to show the total present student(means total of "present checkbox" column checked checkboxes) in a label names total present students."

You don't show any code for doing this, but it's pretty simple: what have you tried so far ?
 
Share this answer
 
v2
Comments
Prashant Arora 31-Jan-16 15:44pm    
Hello sir,
First I would like to thank you to give me this nice suggestion of three states of CheckBoxes. I tried this and found that Indeterminate state is default selected(as I know one state would be default selected for all CheckBoxes). So, I place Checked with "Present", UnChecked with "Absent" and Indeterminate with "On Leave". So, Here all checkBoxes are showing default On Leave. This is not looking good to the person to whom I am going to handover this software.
Please can you explain me the right way of e.ColumnIndex event for one checkedBox in a gridview Row. I have tried Below Code-
private void dgvAttendance_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
for (int i = 0; i < dgvAttendance.Rows.Count - 1; i++)
{
if (e.ColumnIndex == 0)
{
if (e.RowIndex == 0)
{
dgvAttendance.Rows[i].Cells["Absent"].Value=false;
dgvAttendance.Rows[i].Cells["Leave"].Value=false;
}
}
else if (e.ColumnIndex == 1)
{
if (e.RowIndex == 1)
{
dgvAttendance.Rows[i].Cells["Present"].Value=false;
dgvAttendance.Rows[i].Cells["Leave"].Value=false;
}
}
else if (e.ColumnIndex == 2)
{
if (e.RowIndex == 2)
{
dgvAttendance.Rows[i].Cells["Present"].Value=false;
dgvAttendance.Rows[i].Cells["Absent"].Value=false;
}
}
}

2) For My Second Query, I wish to calculate the sum of "True" values( which are values of CheckedCheckBoxes of Present Students) in a single column of the gridview and show sum into a lable.
I tried below ( Here, final is my dataTable and Present is Column)
LblPrsntStudents.Text = final.Compute("sum(Present)", "").ToString();

Please Help. I am very thankful to you

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