Если я запускаю этот код, приложение вылетает, и я не понимаю, почему,
мне кажется, это ошибка - или это так?
Шаги: поместите System.Windows.Forms.DataGridView в форму.
Определить класс источника данных:
public class SomeClass
{
public SomeClass()
{
col1 = string.Empty;
Col2= string.Empty;
}
public string col1 {get;set;}
public string Col2 { get; set; }
}
//Declare new. (0 elements)
private List<SomeClass> _col = new List<SomeClass>();
// Первый запуск этого
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = _col;
}
// Затем запустите
private void button2_Click(object sender, EventArgs e)
{
_col.Add(new SomeClass() { col1 = "Value1", Col2 = "Value2" });
dataGridView1.DataSource = null;
dataGridView1.DataSource = _col;
}
//If you now click in the grid it will crash.
//Note-if i dont set null - it will not be update, if i only run button2 several times
//it works fine !
Исключение:
Blockquote
System.IndexOutOfRangeException не было обработано
Сообщение = Индекс -1 не имеет значения.
Источник = System.Windows.Forms
Трассировки стека:
в System.Windows.Forms.CurrencyManager.get_Item (индекс Int32)
в System.Windows.Forms.CurrencyManager.get_Current ()
в System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter (DataGridViewCellEventArgs e)
в System.Windows.Forms.DataGridView.OnRowEnter (DataGridViewCell & dataGridViewCell, Int32 columnIndex, Int32 rowIndex, логическое значение canCreateNewRow, логическое значение validationFailureOccurred)
в System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (Int32 columnIndex, Int32 rowIndex, логическое значение setAnchorCellAddress, логическое значение validateCurrentCell, логическое значение throughMouseClick)
в System.Windows.Forms.DataGridView.OnCellMouseDown (HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
в System.Windows.Forms.DataGridView.OnCellMouseDown (DataGridViewCellMouseEventArgs e)
в System.Windows.Forms.DataGridView.OnMouseDown (MouseEventArgs e)
в System.Windows.Forms.Control.WmMouseDown (сообщение & m, кнопка MouseButtons, щелчки Int32)
в System.Windows.Forms.Control.WndProc (сообщение & m)
в System.Windows.Forms.DataGridView.WndProc (Message & m)
в System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m)
в System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m)
в System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
в System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg)
в System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtr dwComponentID, причина Int32, Int32 pvLoopData)
в System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (причина Int32, контекст ApplicationContext)
в System.Windows.Forms.Application.ThreadContext.RunMessageLoop (причина Int32, контекст ApplicationContext)
в System.Windows.Forms.Application.Run (Форма mainForm)
в WindowsFormsApplication3.Program.Main () в C: \ TestUtveckling \ WindowsFormsApplication3 \ WindowsFormsApplication3 \ Program.cs: строка 18
в System.AppDomain._nExecuteAssembly (сборка RuntimeAssembly, аргументы String [])
в System.AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args)
в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
в System.Threading.ThreadHelper.ThreadStart_Context (состояние объекта)
в System.Threading.ExecutionContext.Run (ExecutionContext executeContext, обратный вызов ContextCallback, состояние объекта, логическое значение ignoreSyncCtx)
в System.Threading.ExecutionContext.Run (ExecutionContext executeContext, обратный вызов ContextCallback, состояние объекта)
в System.Threading.ThreadHelper.ThreadStart ()
InnerException:
Если я не выполню первый шаг, подключив 0 элементов к источнику данных,
оно работает. Я говорю, что это ошибка в DataGridView! Или это что-то еще?