locked
Windows Forms App (.NET Core 3.1) shows Red Cross for DataGridView RRS feed

  • Question

  • Hello!

    I have a question about the DataGridView control in Windows Forms App(.NET Core 3.1). I use Visual Studio 2019 Preview.

    I have declared the DataGridView programatically in the file Form1.Designer.cs which compiles well.

    But in the Designer view, I can only see a RED cross so I can't iteract with the control there at all. Not even see its properties.

    The DataGridView control is "Grayed Out" in the Toolbox aswell so it is not possible to "Drag-and-Drop" the control on the Form.

    How is it possible to get rid of the RED cross and see the DataGridView and probably also be able to Drag-and-Drop the control?

    



    • Edited by Silvers11 Friday, April 17, 2020 7:10 PM
    Friday, April 17, 2020 7:05 PM

Answers

  • Hi Silvers11
    As this blog said, in .NET Core 3.1 a few outdated Windows Forms controls (DataGrid, ToolBar, ContextMenu, Menu, MainMenu, MenuItem, and their child components) were removed. 
    So you can't add the DataGridView control from the toolbox currently.
    I suggest you can refer to Kareninstructor's first link and add the DataGridView programatically.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Silvers11 Tuesday, April 21, 2020 11:48 AM
    Tuesday, April 21, 2020 7:39 AM

All replies

  • See the following blog post which explain how to get passed this.

    https://gunnarpeipman.com/datagridview-dotnet-core-31/

    And

    https://github.com/dotnet/samples/tree/master/windowsforms/datagridview/CSWinFormDataGridView


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 17, 2020 8:16 PM
  • Thank you Karen, I was finding the first link also when I googled before but on that link they only add the DataGridView programatically in the designer.cs file which is the way I already are doing.

    The second link I am not sure what they mean.

    I am not sure if there is a way to add the DataGridView to the toolbox and/or get rid of the Red cross in the designer view because it is not possible to Edit the control in the Designer view?

    I also get this error message when I try to run the project. The thing is that I am moving all my code from a Windows Forms App .NET Framework application to Windows Forms App .NET Core application.

    The columns are shown/seen in the Designer as they exist programatically in the Designer.cs file. (The solution is have a successfull build)

    But when I "F5" the application and try to run the application, I get the below error and also all the columns are REMOVED from the Designer.cs file which is very strange. The code actually for the columns just dissapear from that file and now I can't see the columns in the Designer View either as the code was removed:

    System.InvalidOperationException: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.'



    • Edited by Silvers11 Friday, April 17, 2020 9:13 PM
    Friday, April 17, 2020 8:55 PM
  • I suggested waiting for a solid release for Windows forms in regards to .NET Core for working with a DataGridView so there are no work arounds. I've been working with Microsoft, specifically with .NET Core and Windows forms and in short it's not ready for prime time at this point in time. The link posted had no guarantees to fix this, just that you are not alone with this issue.

    The second link are official code samples.

    Please in the future post anything you have found, that is what I do (and in 12 years have only asked three questions and each time had pages I found and presented them).



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 17, 2020 9:11 PM
  • In regards to this

    The best way to move forward with a project is not to start with porting from .NET Framework to .NET Framework Core. Instead write smaller project to test if "things" work. Also you can use GitHub repository where the main branch is the current project and a new branch is the .NET Core project if you really want to port correctly without writing smaller test projects. This means you have both with backups too.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 17, 2020 9:15 PM
  • I did a fast test program with only a DataGridView and was able to run the application and see the DataGridView when debugging with F5. I actually did miss a line of code there so That was a very big step. Thank you!

    But still then I will come back to the main problem that I can't understand what I need to do in order to get the DataGridView in the Toolbox so it is possible to "Drag-and-Drop" the DataGridView on the Form directly and interact with the properties from Design View.

    I am not sure if this is possible to do?



    • Edited by Silvers11 Friday, April 17, 2020 9:35 PM
    Friday, April 17, 2020 9:34 PM
  • Hello,

    It's not possible to add the DataGridView control from the IDE toolbox currently.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 17, 2020 9:54 PM
  • But still then I will come back to the main problem that I can't understand what I need to do in order to get the DataGridView in the Toolbox so it is possible to "Drag-and-Drop" the DataGridView on the Form directly and interact with the properties from Design View.

    Sure you can do it. You can make the control manually. 

    Friday, April 17, 2020 9:59 PM
  • Yes, that is interesting. I did find something about this also and found a class like this that I have added to the solution(DataGridViewPlus.cs) which automatically have appeared in the toolbox.

    But when I try to drag and drop the control, I get an MessageBox Error like below. I am not sure if I do this incorrectly or if it is actually possible to create a DataGridView control and add it to the toolbox?

    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Artificial_Intelligence
    {
        public class DataGridViewPlus : DataGridView
        {
            /// 
            /// This prevents the "red X" error which happens when you cause updates to a datagrid
            /// from multiple threads. It catches the OnPaint() exception and invalidates
            /// the grid so it gets redrawn the next time the application hits its message loop.
            ///
            /// The following solution to this problem was found at
            /// http://social.msdn.microsoft.com/forums/en-US/winforms/thread/fdd94896-80e9-4e91-9ed5-0348bf2633a9
            ///
            protected override void OnPaint(PaintEventArgs e)
            {
                try
                {
                    base.OnPaint(e);
                }
                catch
                {
                    Invalidate();
                }
            }
        }
    }



    • Edited by Silvers11 Saturday, April 18, 2020 10:09 PM
    Saturday, April 18, 2020 10:08 PM
  • Hi Silvers11
    As this blog said, in .NET Core 3.1 a few outdated Windows Forms controls (DataGrid, ToolBar, ContextMenu, Menu, MainMenu, MenuItem, and their child components) were removed. 
    So you can't add the DataGridView control from the toolbox currently.
    I suggest you can refer to Kareninstructor's first link and add the DataGridView programatically.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Silvers11 Tuesday, April 21, 2020 11:48 AM
    Tuesday, April 21, 2020 7:39 AM