简体   繁体   中英

Adding new row in the datagridview hangs the application C# form

I have an application that is sending new values to be added in the datagridview. But it seems after a certain time when data goes beyond the form size it is hanging the application. I have checked I am not even able to scroll down.

Here is the the function that is adding row in the 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()));
        }

    }

Please let me know if there is any way to not bind the datagrid with some source and still it function properly. I have thought of using database, but that is not allowed to me. It is dynamic always.

Please suggest me efficient methods.

I guess you are doing it the wrong way. I think you are using the form in separate thread and trying to add through separate thread then this might have hanged your application.

Try this, I hope it will help you:

[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()));
        }

    }

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM