【问题标题】:Adding new row in the datagridview hangs the application C# form在 datagridview 中添加新行会挂起应用程序 C# 表单
【发布时间】:2020-05-28 10:04:01
【问题描述】:

我有一个应用程序正在发送要添加到数据网格视图中的新值。但似乎在一段时间后,当数据超出表单大小时,它会挂起应用程序。我检查过我什至无法向下滚动。

这是在 datagridview 中添加行的函数:

 [DllExport("AddToGrid", CallingConvention = CallingConvention.StdCall)]
    public static void AddToGrid(
        [MarshalAs(UnmanagedType.LPWStr)] string formhandle,
        [MarshalAs(UnmanagedType.LPWStr)] string grid_name,
        [MarshalAs(UnmanagedType.R8)] double status,
        [MarshalAs(UnmanagedType.R8)] double no,
        [MarshalAs(UnmanagedType.R8)] double test1,
        [MarshalAs(UnmanagedType.R8)] double percentage,
        [MarshalAs(UnmanagedType.R8)] double marks,
        [MarshalAs(UnmanagedType.R8)] double grade
        )
    {
        try
        {
            DataGridView dataview = null;
            Control control = null;
            GuiController controller = m_controllers[formhandle];
            if(!controller.m_controls.TryGetValue(grid_name, out control))
            {
                SendExceptionEvent(formhandle, new Exception("could not find the grid: "+grid_name));
                return;
            }
            dataview = (DataGridView)control;
            string status_string = "E";
            if (status == 0 || status == 1) status_string = "A";
            if (status > 1 && status<6) status_string = "P";
            if (status == 6) status_string = "V";

            dataview.Rows.Add(status_string, no.ToString(), test1.ToString(), percentage.ToString(), marks.ToString(), grade.ToString());
        }
        catch(Exception e)
        {
            MessageBox.Show("Issues adding value: ", e.ToString());
            SendExceptionEvent(formhandle, new Exception("Issues adding value: "+ e.ToString()));
        }

    }

请让我知道是否有任何方法可以不将数据网格与某些源绑定并且仍然可以正常运行。我曾想过使用数据库,但这对我来说是不允许的。它总是动态的。

请给我建议有效的方法。

【问题讨论】:

  • 请有人告诉我我能做些什么来解决这个问题。如果 datagridview 不正确,那么我可以使用哪个组件来显示运行时更改数组?

标签: c# datagridview


【解决方案1】:

我猜你做错了。我认为您在单独的线程中使用表单并尝试通过单独的线程添加,那么这可能会挂起您的应用程序。

试试这个,希望对你有帮助:

[DllExport("AddToGrid", CallingConvention = CallingConvention.StdCall)]
    public static void AddToGrid(
        [MarshalAs(UnmanagedType.LPWStr)] string formhandle,
        [MarshalAs(UnmanagedType.LPWStr)] string grid_name,
        [MarshalAs(UnmanagedType.R8)] double status,
        [MarshalAs(UnmanagedType.R8)] double no,
        [MarshalAs(UnmanagedType.R8)] double test1,
        [MarshalAs(UnmanagedType.R8)] double percentage,
        [MarshalAs(UnmanagedType.R8)] double marks,
        [MarshalAs(UnmanagedType.R8)] double grade
        )
    {
        try
        {
            DataGridView dataview = null;
            Control control = null;
            GuiController controller = m_controllers[formhandle];
            if(!controller.m_controls.TryGetValue(grid_name, out control))
            {
                SendExceptionEvent(formhandle, new Exception("could not find the grid: "+grid_name));
                return;
            }
            dataview = (DataGridView)control;
            string status_string = "E";
            if (status == 0 || status == 1) status_string = "A";
            if (status > 1 && status<6) status_string = "P";
            if (status == 6) status_string = "V";
            control.Invoke((MethodInvoker)delegate
                {
                    dataview.Rows.Add(status_string, no.ToString(), test1.ToString(), percentage.ToString(), marks.ToString(), grade.ToString());
                });
        }
        catch(Exception e)
        {
            MessageBox.Show("Issues adding value: ", e.ToString());
            SendExceptionEvent(formhandle, new Exception("Issues adding value: "+ e.ToString()));
        }

    }

希望对你有帮助。

【讨论】:

  • 哇,我从来没想过。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2016-06-28
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多