Hi @Pat Hanks , Welcome to Microsoft Q&A,
Did you forget to set the target column to be the checkbox column?
I created a BindingList with Boolean properties as a demonstration. Use the AddButton demo to add a row with default false. Use EditButton to demonstrate modifying the boolean property of the target cell.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace _8_29_x
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public PersonBindingList people = new PersonBindingList();
private void Form1_Load(object sender, EventArgs e)
{
people.Add(new Person(1, "a", true));
people.Add(new Person(2, "b", false));
people.Add(new Person(3, "c", true));
dataGridView1.DataSource = people;
}
private void AddButton_Click(object sender, EventArgs e)
{
people.Add(new Person(people.Count + 1, "New Person", false));
}
private void EditButton_Click(object sender, EventArgs e)
{
Boolean adult = (Boolean)dataGridView1.CurrentRow.Cells["adult"].Value;
dataGridView1.CurrentRow.Cells["adult"].Value = !adult;
}
}
public class Person : INotifyPropertyChanged
{
public int Id { get; set; }
private String _name;
public String Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged(nameof(Name));
}
}
private Boolean _adult;
public Boolean Adult
{
get { return _adult; }
set
{
_adult = value;
NotifyPropertyChanged(nameof(Adult));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public Person(int id, string name, bool adult)
{
Id = id;
Name = name;
Adult = adult;
}
}
public class PersonBindingList : BindingList<Person>
{
protected override void OnListChanged(ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemAdded)
{
// Automatically add a new row in the DataGridView when a new item is added
if (IsSortedCore) // Check if sorting is applied
{
ApplySortCore(SortPropertyCore, SortDirectionCore); // Reapply sorting to include the new item
}
}
base.OnListChanged(e);
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.