3

first timer here on Stack Overflow (although I've been lurking for ages).

I'm developing a little application which contains two DataGridViews. The second DataGridView is filled via a binding on a list of a custom class objects (the user presses a button and the list is added with a new element and re-binded to the DataGridView).

The problem I'm facing is that when I have some rows in this DataGridView (even if it's only one row in fact...), if I happen to click one of the rows to select them, Visual Studio pops up showing me the debugger, because a System.IndexOutOfRangeException happened.

It seems that when the user clicks on a row, the DataGridView throws this exception because it says I'm trying to access the -1 index of the array.

The strange fact is that this exception is thrown even if I don't have any event listening for the row selection!

Actually, there is no event at all listening on the DataGridView.

Debugger isn't helping because it's throwing the exception at the Form constructor level (it's breaking at the Application.Run(new frmMain()); so it's not telling me anything useful).

Can anybody help me please?

If you need any code let me know, and I'll provide you what I can.

Thanks!

Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
frikyfriky11
  • 73
  • 1
  • 8
  • Its calling something somewhere allright, what's in your initializer/form load – EpicKip Apr 14 '17 at 13:30
  • Post code, if you've been lurking here, you know what we are going to say. I'm happy to help, but we really do need to see what is happening in the code. – Trey Apr 14 '17 at 13:30
  • @EpicKip what do you mean? Do you want me to post the `Load` event and the `constructor` of the Form? – frikyfriky11 Apr 14 '17 at 13:32
  • 1
    @Trey I would be so happy to show you the code, if only I knew what to show you... what can I post? – frikyfriky11 Apr 14 '17 at 13:33
  • @frikyfriky11 Somewhere you are accessing the index, might be by accident but its not saying it for nothing. – EpicKip Apr 14 '17 at 13:41
  • @EpicKip I'm trying to figure out where I'm accessing the index, but I can't help with it because I'm sure I'm not accessing it on click of the rows of the DataGridView... – frikyfriky11 Apr 14 '17 at 13:53
  • No code = not reproduce able. Can't help you. – EpicKip Apr 14 '17 at 13:57
  • You have two grids.. show us how your adding them to the form, their properties, ANY code that touches them... it's likely a binding issue. I'm also interested in any and all events. – Trey Apr 14 '17 at 14:20
  • @Trey your comment made me think about bindings... right now I'm testing if it's a problem of bindings or not, will update as soon as I figure out if they're the problem. – frikyfriky11 Apr 14 '17 at 14:33

1 Answers1

3

First of all, thanks for the feedbacks.

Many Thanks to user @Trey who made me think that I had to double check my bindings.

I was binding my DataGridViews directly to my Lists<CustomType>, and not using a BindingList.

My code before:

initialization:

List<CustomType> myList = new List<CustomType>();
// populating the list with some code, skipping because not relevant
myDataGridViewExample.DataSource = myList;

data insertion:

myList.Add(something);
myDataGridViewExample.DataSource = null;
myDataGridViewExample.DataSource = myList;

in this way, I was rebinding the list directly everytime I updated the list

My code now:

initialization:

List<CustomType> myList = new List<CustomType>();
// populating the list with some code, skipping because not relevant
BindingList<CustomType> myBind = new BindingList<CustomType>(myList);
myDataGridViewExample.DataSource = myBind;

data insertion:

myList.Add(something);
myBind.ResetBindings();

in this way, I am only refreshing the BindingList and not touching directly the List itself.

This seem to have solved my problem, but I will edit the answer if I encounter other strange behaviours.

Thanks again, have a nice day! :)

Community
  • 1
  • 1
frikyfriky11
  • 73
  • 1
  • 8