I have a DataGridView bound to an XML file, then I add an unbound column of checkboxes to the DGV.

private void btnReconcile_Click(object sender, EventArgs e)
            DataSet ds = new DataSet();

            DataSet modds = new DataSet();
            DataTable dt = new DataTable();
            dt = ds.Tables[0].Clone();

            foreach (DataTable tbl in ds.Tables)
                progRecords.Value = 0;
                progRecords.Minimum = 0;
                progRecords.Maximum = tbl.Rows.Count;
                foreach (DataRow dr in tbl.Rows)
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "SELECT COUNT(*) FROM Contacts WHERE email1 LIKE '%" + dr["Email"].ToString() + "%'";
                        nCount = (int)myCommand.ExecuteScalar();
                        if (nCount <= 0)
                    catch (Exception ex)


            dataGridView1.DataSource = modds.Tables[0];
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

            MessageBox.Show("Verification Complete!");

And the code to create the column is as follows.

private void CreateUnboundButtonColumn()
            if (columnAdded == true) return;
            // Initialize the button column.
            DataGridViewCheckBoxColumn checkBoxColumn =
                new DataGridViewCheckBoxColumn();
            checkBoxColumn.Name = "Add";
            checkBoxColumn.HeaderText = "Add";
            checkBoxColumn.TrueValue = false;
            checkBoxColumn.FalseValue = true;
            dataGridView1.AutoGenerateColumns = false;

            // Use the Text property for the button text for all cells rather
            // than using each cell's value as the text for its own button.

            // Add the button column to the control.
            dataGridView1.Columns.Insert(0, checkBoxColumn);
            columnAdded = true;

In my data source, I have a node that is set to either "true" or "false". How can I get the checkbox to be bound to that node?

Also, side note, I'm not sure that my DataGridViewCellEventHandler is correct. So, would I have the data source be modified in the event handler?


Recommended Answers

All 2 Replies

Have a look at code,

DataTable dt = new DataTable();

            dataGridView1.AutoGenerateColumns = false;

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            chk.DataPropertyName = "Column1";


            dataGridView1.DataSource = dt;

Thanks for your reply, but it was not what I needed.

Instead of your solution where you add the column first then bind the datasource, I binded the datasource first and then added a column. I also made an event where if a checkbox was clicked, then the datasource would be edited so that it could maintain the checkbox.

So, basically, I just added another childnode to the XML document indicating whether it was checked or not (set to false by default), and then the event handler would change the node when the box is checked.

void dataGridView1_Sorted(object sender, EventArgs e)
            foreach (DataGridViewRow dr in dataGridView1.Rows)

                dr.Cells[0].Value = dr.Cells[18].Value;

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            if (e.ColumnIndex == 0)
                dataGridView1[18, e.RowIndex].Value = dataGridView1[0, e.RowIndex].Value.ToString();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.