This works for me with the following code.
Sorry for the rough code - I am just showing key snippets, but I can provide a more complete example if you need to.
I have a SortableBindingList _names bound to my DataGridView. Then in my form I have a button with new names added to the Click handler even. This works great to add the name kevin between joe and pete.
private SortableBindingList<Names> _names; public Form1() { InitializeComponent(); _names = new SortableBindingList<Names>(); _names.Add(new Names() { Name = "joe" }); _names.Add(new Names() { Name = "pete" }); DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); col1.DataPropertyName = "Name"; dataGridView1.Columns.Add(col1); dataGridView1.DataSource = _names; } private void button1_Click(object sender, EventArgs e) { _names.Add(new Names(){Name = "kevin"}); dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending); } public class Names { public string Name { get; set; } }
So, the main thing is that I sort my DataGridView after adding to the list.
I could also provide IComparer in my .Sort () call - the default comparator simply compares with .ToString ()
Interestingly, in my example, when inserting an element, the following also works:
private void button1_Click(object sender, EventArgs e) {
Just pasting an element in the right place is enough for the grid to display a list sorted correctly. I use the same SortableBindingList as you, the one shown on MartinWilley.com.
Could your problem be what you add, not paste?
source share