All,

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();
            ds.ReadXml(txtPath.Text);

            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)
                {
                    progRecords.Value++;
                    try
                    {                        
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "SELECT COUNT(*) FROM Contacts WHERE email1 LIKE '%" + dr["Email"].ToString() + "%'";
                        nCount = (int)myCommand.ExecuteScalar();
                        if (nCount <= 0)
                        {
                            WriteContactToXML(dr);                           
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }

                }
            }

            modds.ReadXml("\\WriteContacts.xml");
            
            dataGridView1.DataSource = modds.Tables[0];
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

            CreateUnboundButtonColumn();
            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?

Thanks,
Andrew

Have a look at code,

DataTable dt = new DataTable();
            dt.Columns.Add("Column1");
            dt.Rows.Add("true");
            dt.Rows.Add(false);
            dt.Rows.Add(true);

            dataGridView1.AutoGenerateColumns = false;

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

            dataGridView1.Columns.Add(chk);

            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;
            }
            dataGridView1.Refresh();
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.Refresh();
            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, networking, learning, and sharing knowledge.