Search code examples
c#winformsdatagridviewdatagridviewcheckboxcell

How to populate CheckBox column with Check in DataGridView if record exists?


I have a DataGridView that only displays 2 columns: Employee Name, and a checkbox called Attended. This is to keep track of employee attendance at safety meetings.

When this DGV is loaded, I would like the employees who have already been marked as having attended to have a check mark in their respective checkboxes.

This is what I tried, but it just checks ALL boxes:

private void LoadEmpAttendanceDGV()
{
    string sqlstm = "SELECT EmpID, EmpName FROM dbo.MY_TABLE";
    // Additional code here, which loads the grid.

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        myKey.empID = tempEmpID;
        myKey.trainingID = tempTrainingID;
        myRow = trainingSession.Read(myKey);

        // Check to see if record exists, meaning employee attended meeting.
        if (myRow != null)
        {
            for (int j=0; j < ds.Tables[0].Rows.Count; j++)
            {
                myDataGridView["Attended", j].Value = true;
                // Where "Attended" is the name of my CheckBox column
            }
        }
    }
}

If anyone can offer any insight, I would greatly appreciate it.


Solution

  • Create tables like this

    SqlServer Code

    USE [acc]
    GO
    
    CREATE TABLE [dbo].[EmpAttendedTheMeeting](
        [EName] [varchar](50) NOT NULL
    ) ON [PRIMARY]
    
    GO
    
    CREATE TABLE [dbo].[Employees](
        [EName] [varchar](50) NOT NULL
    ) ON [PRIMARY]
    
    GO
    insert into Employees(EName) values('kashif')
    insert into Employees(EName) values('sunny')
    insert into Employees(EName) values('kamran')
    
    insert into EmpAttendedTheMeeting(EName) values('kashif')
    insert into EmpAttendedTheMeeting(EName) values('kamran')
    

    c# Code

    namespace WindowsFormsApplication9
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            DataGridViewTextBoxColumn tb = new DataGridViewTextBoxColumn();
            DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
            private void Form1_Load(object sender, EventArgs e)
            {
                FormatGrid(dataGridView1);
                LoadDg();
            }
            private void FormatGrid(DataGridView dg)
            {
                dg.Columns.Add(tb);
                dg.Columns[0].HeaderText = "EName";
                dg.Columns[0].Width = 199;
    
                dg.Columns.Add(cb);
                dg.Columns[1].HeaderText = "Attended";
                dg.Columns[1].Width = 69;            
            }
            private void LoadDg()
            {
                SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=acc;uid=sa;pwd=emotions");                
                SqlDataAdapter da = new SqlDataAdapter("select Employees.EName, EmpAttendedTheMeeting.EName from Employees left join EmpAttendedTheMeeting on Employees.EName = EmpAttendedTheMeeting.EName", cn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {           
                    dataGridView1.Rows.Add(dr[0], dr[1].ToString() == string.Empty ? false : true);
                }
            }   
    
        }
    }
    

    Im getting the result in my gridview like this. because sunny is not in EmpAttentendedTheMeeting, his name is unchecked

    gridview