Windows form application exception

前端 未结 3 407
眼角桃花
眼角桃花 2020-12-19 05:57

I get application exception

   at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
   at System.Windows.Forms.CurrencyManager.get_Current()
   at S         


        
相关标签:
3条回答
  • 2020-12-19 06:10

    I'm guessing that you have bound a List that is initially empty, (or other sort of collection that does not generate list changed events) to your DataGridView, and then added items to this List.

    The items you add will display correctly on your grid, but clicking on a row will cause this exception. This is because the underlying CurrencyManager will be reporting its current row position as an offset of -1. It will stay this way because the List does not report changes to the grid.

    You should only bind your list to the grid if it has some items in it to begin with, or rebind when you add them.

    See also my answer to this question, which is essentially the same problem.

    0 讨论(0)
  • 2020-12-19 06:20

    Following Andy's advice I substituted

    private List<Employee> Employees { get; set; } = new List<Employee>(); _employeesGridView.DataSource = Employees;

    with

    private BindingList<Employee> Employees { get; set; } = new BindingList<Employee>(); _employeesGridView.DataSource = Employees;

    and the problem disappeared.

    0 讨论(0)
  • 2020-12-19 06:36

    After lots of effort i have solved this issue. Bind your DataGridView When you have some data in the Data Source either it would be a list or DataTable.

    0 讨论(0)
提交回复
热议问题