Reputation: 140
I'm binding some fields to a datagridview with
myDataGridView.datasource = List(of MyCustomObject)
And i never touch the indexes manually but i'm getting -1 index have no value (so, an out of bound error)
The error comes on the onRowEnter() Event when I select any row in the grid. It doesn't even enter any of my events for handling my clic, it bugs before...
On the interface,just before clicking, I can see that no rows of grid 2 is the (currentRow). I guess the bug comes from there but I don't know how to set a currentRow Manually or simply avoid this bug...
Any one have seen this before?
edit - I've added some code to test:
dgvAssDet.CurrentCell = dgvAssDet.Rows(0).Cells(0)
When this assignation runs, the currentCell = nothing, there is > 5 rows and columns...
And i've got the same error, before changing the old currentCell it trys to retrieve it, but in my case there is none and it bugs... I've got no idea why.
Upvotes: 2
Views: 1354
Reputation: 3611
When you use a DataSource on a DGV, you should use the collection System.ComponentModel.BindingList<T>
, not System.Collections.Generic.List<T>
public class MyObject
{
public int Field1 { get; set; }
public MyObject() { }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataGridViewTest();
}
public void DataGridViewTest() {
BindingList<MyObject> objects = new BindingList<MyObject>();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = objects;
dataGridView1.AllowUserToAddRows = true;
objects.Add(new MyObject() {
Field1 = 1
});
}
}
If your objects list was the type of List<T>
you would get the index out of bounds error.
Upvotes: 2