c# - DataGridView 自动添加新行 - 为什么?

标签 c# .net winforms datagridview

您好,我有一个使用 Windows 窗体的旧 C# 应用程序编写得非常糟糕。我最近不得不对应用程序进行一些广泛的更改,现在我面临着一个非常奇怪的问题。

该应用程序最初设计用于在主窗口 OnLoad 方法中加载数据(我知道这个想法很糟糕),后来重构为在另一个线程中不显眼地加载数据。该应用程序使用 BindingListView ( http://blw.sourceforge.net/ ),它是 IBindingListView 接口(interface)的非常简单的实现。

在我对应用程序进行更改之前,一切正常。应用程序启动时加载了所有数据,没有任何问题(在等待应用程序加载数据时忽略了糟糕的用户体验)。

但是当我将应用程序更改为在另一个线程中动态加载数据时(网格首先显示为空,然后将数据加载到其中),开始弹出一些空行,这些空行不在绑定(bind)到的 ObservableCollection 中网格。

我设法跟踪到此调用堆栈的新添加行:

     Equin.ApplicationFramework.BindingListView.dll!Equin.ApplicationFramework.AggregateBindingListView<BusinessLogic.DataEntries.TrackEntry>.AddNew() Line 295 C#
    Equin.ApplicationFramework.BindingListView.dll!Equin.ApplicationFramework.AggregateBindingListView<BusinessLogic.DataEntries.TrackEntry>.System.ComponentModel.IBindingList.AddNew() Line 1617 + 0x8 bytes  C#
    System.Windows.Forms.dll!System.Windows.Forms.BindingSource.AddNew() + 0xbd bytes
    System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.AddNew() + 0x26 bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew() + 0x86 bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded() + 0x24 bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnRowEnter(ref System.Windows.Forms.DataGridViewCell dataGridViewCell = null, int columnIndex = 0, int rowIndex = 0, bool canCreateNewRow, bool validationFailureOccurred) + 0x10a bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(int columnIndex = 0, int rowIndex = 0, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick = false) + 0x59f bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(int columnIndex = 0, int rowIndex = 0, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick, bool clearSelection = true, bool forceCurrentCellSelection = false) + 0x2a bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(bool includeNewRow) + 0x66 bytes
    System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnEnter(System.EventArgs e) + 0xe0 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.NotifyEnter() + 0x20 bytes
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.UpdateFocusedControl() + 0xc6 bytes
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control value = {System.Windows.Forms.DataGridView}) + 0x54 bytes
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control control, bool originator = false) + 0x76 bytes
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControlInternal(System.Windows.Forms.Control value = {System.Windows.Forms.DataGridView}) + 0x73 bytes
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControl(System.Windows.Forms.Control ctl) + 0x33 bytes
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActiveControl.set(System.Windows.Forms.Control value) + 0x5 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.Select(bool directed, bool forward) + 0x1b bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.Select() + 0xf bytes
    System.Windows.Forms.dll!System.Windows.Forms.SplitContainer.SelectNextActiveControl(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x2c bytes
    System.Windows.Forms.dll!System.Windows.Forms.SplitContainer.SelectNextControlInContainer(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0xdc bytes
    System.Windows.Forms.dll!System.Windows.Forms.SplitContainer.Select(bool directed, bool forward) + 0x65 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControl(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x7b bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControlInternal(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x4a bytes
    System.Windows.Forms.dll!System.Windows.Forms.Form.Active.set(bool value) + 0x11d bytes
    System.Windows.Forms.dll!System.Windows.Forms.Form.WmActivate(ref System.Windows.Forms.Message m) + 0x42 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) + 0x146 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg = 6, System.IntPtr wparam, System.IntPtr lparam) + 0x5a bytes
    [Native to Managed Transition]
    [Managed to Native Transition]
    System.Windows.Forms.dll!System.Windows.Forms.Control.SetVisibleCore(bool value = true) + 0x333 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool value = true) + 0x87 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.Visible.set(bool value) + 0x11 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0xe8 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes
    Registr optické komunikace.exe!GUI.Program.Main() Line 29 + 0x1d bytes  C#
    [Native to Managed Transition]
    [Managed to Native Transition]
    mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection.Assembly assembly, string[] args) + 0x19 bytes
    mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel) + 0x6e bytes
    mscorlib.dll!System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() + 0x84 bytes
    mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext, string[] activationCustomData) + 0x65 bytes
    mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext) + 0xa bytes
    mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext) + 0x3e bytes
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() + 0x23 bytes
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes

这让我怀疑,添加新的不需要的行是以某种方式单独在 DataGridView 中启动的,没有明确编写代码来执行此操作。

所以我的问题是 - DataGridView 可以这样做吗?以及如何关闭它?我的下一个问题也是,如果 DataGridView 只是在处理之前向它发出的一些命令,我​​如何跟踪触发 BindingListView 的 AddNew 方法的是什么?

最佳答案

设置属性AllowUserTAddRows=false; 或通过代码

myDataGridView.AllowUserToAddRows = false;

关于c# - DataGridView 自动添加新行 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5593059/

相关文章:

c# - 以编程方式链接所有点

c# - DebuggerDisplay - 在方括号中键入

c# - 如何使用 BuildManager 在 Visual Studio 2017 (MsBuild 15) 上构建 .Net Core 项目或解决方案

.net - 在正在运行的进程中检测类的未处理实例?

.net - VB6 App + .Net 组件作为编译应用程序工作,但不在 VB6 IDE 中工作

sql - linq 用于连接两个表

c# - C#线程和属性第二部分

c# - 使用转义序列连接两个字符串

c# - 使用c#解析xml

c# - 为什么 Type.GetType 给出 null?