none
DataGridView Binding Problem: Rows are all empty RRS feed

  • Question

  • I am binding a business object that inherits from BindingList(Of T) to the DataGridView.

     

    When I bring up the form, the correct number of (empty) rows are displayed, but there is no data.

     

    In the debugger, I look at the DataPropertyName for each column, and they are correct.

    I look at the Rows(index).DataBoundItem, and it is correct!!!

    But the Value property of each cell is Nothing and no data appears in the grid.

     

    How can the row have a valid DataBoundItem, the column have a valid DataPropertyName, but the cell have no value?

     

    I tried every style of refresh/reset that I could think of to no avail.

     

    One additional important point ... If I run this form as the startup form of my project IT DOES WORK. It only does NOT work when I bring is up from another form, which is a custom Windows Forms designer.

     

    Any hints would be appreciated!!

    Saturday, August 16, 2008 12:54 AM

All replies

  • I am hosting the Windows Forms designer in one of my dialogs. I have a set of menu options at the top of this dialog that displays other dialogs (for editing configuration type data).

     

    These dialogs work fine when I run them directly. But when I run them from the designer dialog, the binding does not work.

     

    Both dialogs contain DataGridView controls that are bound using object binding.

     

    Again, they work fine if I run them directly. They also run fine from the designer dialog *IF* I comment out the code that sets up the design surface.

     

    On the dialogs, the grid appears and seems to have the correct number of rows. But the binding shows no data in any of the columns.

     

    Is there a way to get these dialogs to work correctly displayed from my designer dialog?

    Friday, August 15, 2008 9:02 PM
  • Hi DeborahK,

     

    Based on my understanding, the problem you are facing is “The rows of DataGridView are empty when you use another form to launch the form which hosts the DataGridView”.

     

    I have tried  both the two ways you described, but the application runs well.  Actually I can’t reproduce your problem.

     

    I think you can first try the following general troubleshooting steps:

    1.    Please apply the latest service pack of Visual Studio.

    2.    Try to create a new project, and see whether the problem still exists. Sometimes corrupted project settings can cause problems. These are project specific.

    3.    Try to reboot the operating system to safe mode and see whether the problem still happens. This can help to isolate other applications are interfering Visual Studio.

    4.    Try to create a new account and test the project in the new account. This can isolate user profile corruption related issues.

    5.    Try to disable Add-ns(e.g. “Tools” | “Add-in Manager”) and run “devenv.exe /safemode”. This can eliminate the possibility that third party Add-ins is causing problems.

     

    If the problem still exists, I hope you can provide the necessary codes which can reproduce the problem, so it will help a lot to find the problem.

     

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Tuesday, August 19, 2008 7:41 AM
  • Your understanding is missing one key item: The dialog launching my form is a custom designer.

     

    Let me restate. It works under the following conditions:

     

    1) If I run the form as a startup form for the project.

    2) If I run the form from a menu on another form.

     

    It does NOT work under the following conditions:

     

    3) If I run the form from a menu on a CUSTOM DESIGNER.

     

    I have had other members of the team try it and they get the same results. Some of us are running SP1, others are not. Some of us are XP, others are Vista.

     

    It is DEFINITELY not a confirguration issue.

     

    If you want to attempt to reproduce the problem, please try building a custom designer and then try calling a form bound to a DataGridView. (Either from the menu or from the Property Window.)

     

    Do you know if custom designers somehow interfere with modal dialogs opened from the designer? Does the designer expect the dialog to somehow be part of what is designed? (Meaning that it brings up the dialog in "design mode" and therefore does not show the data?)

     

    THANKS!

    Tuesday, August 19, 2008 3:06 PM
  •  DeborahK wrote:

    Your understanding is missing one key item: The dialog launching my form is a custom designer.

     

    Let me restate. It works under the following conditions:

     

    1) If I run the form as a startup form for the project.

    2) If I run the form from a menu on another form.

     

    It does NOT work under the following conditions:

     

    3) If I run the form from a menu on a CUSTOM DESIGNER.

     

    I have had other members of the team try it and they get the same results. Some of us are running SP1, others are not. Some of us are XP, others are Vista.

     

    It is DEFINITELY not a confirguration issue.

     

    If you want to attempt to reproduce the problem, please try building a custom designer and then try calling a form bound to a DataGridView. (Either from the menu or from the Property Window.)

     

    Do you know if custom designers somehow interfere with modal dialogs opened from the designer? Does the designer expect the dialog to somehow be part of what is designed? (Meaning that it brings up the dialog in "design mode" and therefore does not show the data?)

     

    THANKS!

     

    Hi DeborahK,

    Thanks for your clarification! As you said, I tried to use Windows form hosting a custom designer, I added event handler for the designer and adding code there to show the databound form. However, the DataGridView was still populated with the datasource. Every row contains data. Although test environment may not be the cause, I still would like to tell you that I am using Visual Studio 2008 under Windows 2003 Enterprise Edition. If you can provide more details, it will help me to find the cause.

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Wednesday, August 20, 2008 2:26 PM
  • Does your custom designer include the menu service?

     

    Me.AddService(GetType(IMenuCommandService), New MenuCommandService(Me))

     

    I wonder if it could be impacting the dialogs?

     

    It seems that the popup dialog could be somehow in design mode? Hence the fact that it is not showing its data?

     

    Is there any way to determine this?

    Wednesday, August 20, 2008 4:21 PM
  • Another question ... are you using an object datasource or a database? I am using a class that inherits from BindingList(Of T)

     

    Wednesday, August 20, 2008 4:23 PM
  • Interesting ... I just changed my datasource to a datatable and it worked!!!

     

    So the problem ONLY occurs when binding to a class that inherits from BindingList(of T).

    Wednesday, August 20, 2008 5:57 PM
  • Hi DeborahK,

    The problem is indeed puzzling. I had included the menu service and the datasource I used to bind to the DataGridView was business object inherited from BindList<T>.

    I got to know that you had tried to use refresh/reset and many ways, but I want to know where you put the binding logic. Does the binding logic in the constructor or load event handler or other? If the binding logic was not in the constructor, you can try to move it into the constructor just after InitializeComponent method and test, and see whether the problem can be solved.

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

      

     

    Thursday, August 21, 2008 3:25 AM
  • Interestingly enough, I think I am experiencing the same problem. I've got a DataGridView on a ToolForm (docker window), whose DataSource is a BindingList<MyClass> object. As items are added/removed to/from the collection, the DataGridView displays the proper number of rows, albeit they appear empty. I also have DataPropertyName set on the columns for binding. But interestingly enough, the moment I click the pin icon to slide the ToolForm off to the side, the DataGridView suddenly starts displaying the data properly, and will continue to do so for the remainder of time that the app is running.

     

    Myself and another developer, prior to discovering this, had created a sample app that was set up almost identically, except the controls were all on one form (no ToolForm involved), and could not duplicate the issue. So we're left with the impression that the ToolForm is meddling somehow, perhaps with events? (Bruce, perhaps you could adjust your experiment by using ToolForm to see if your sample app continues to work properly?)

     

    I have no solution to offer at this point, so I do apologize. But I will be watching this conversation to see if you guys come up with anything. And if I discover something, I will be sure to post my findings.

     

     

    Thursday, August 21, 2008 6:16 PM
  • Thank you for chimming in. I'm glad I am not crazy!

     

     

    I'm going to see if there is an easy way to remove the toolbox from my app and see what that does.

     

    Thanks!

    Thursday, August 21, 2008 6:21 PM
  • I commented out all of the code that set up the Toolbox including my Toolbox class and the call to add the IToolboxService and it didn't make a difference. It still does not display the data.

     

    But I agree that it *has* to be something in the designer itself.

     

    My thought was the somehow the designer is telling the pop up form that it is in design mode, and therefore should not set its data. But I checked the DesignMode property and it appears to be false.

     

    So I am out of ideas and getting a little desperate.

    Thursday, August 21, 2008 6:35 PM
  • In my scenario, MyClass implements the ICustomTypeDescriptor interface (so that I can control what is displayed in the properties grid). When I adjust MyClass to not implement that interface, I no longer encounter this problem.

    Friday, August 22, 2008 3:02 PM
  • I am not implementing that interface. But I do have quite a few property editors coming up from the PropertyGrid. Don't know if one of them is causing the problem?

    Friday, August 22, 2008 4:13 PM
  • I just commented out all of the code I had that messed with the PropertyGrid (filtered properties and set Propery Editors). AND IT NOW WORKS.

     

    So, if Bruce is still around, maybe he can try adding a Property Editor to the Property Grid of his designer and see if he can reproduce the problem.

     

    In the mean time, I am going to slowing uncomment some of this code to see if I can pinpoint the issue.

     

    BUT THIS SEEMS LIKE A .NET BUG!!!!

     

    Friday, August 22, 2008 4:25 PM
  • I narrowed it down to one line of code:

    Code Snippet

     

    ' Iterate through the list

    For Each pd As PropertyDescriptor In TypeDescriptor.GetProperties(Me)

      Dim attr As BrowsableAttribute = TryCast(pd.Attributes(BrowsableAttribute.Default.GetType), BrowsableAttribute)

      Dim fld As FieldInfo

      fld = attr.GetType.GetField("browsable", BindingFlags.Instance Or BindingFlags.NonPublic)

      If ddControl.Properties.Contains(pd.Name) Then

         ' Modify the entry to turn on the Browsable attribute.

          fld.SetValue(attr, True)

       Else

          ' Modify the entry to turn off the Browsable attribute.

          fld.SetValue(attr, False)

       End If

    Next

     

     

     

    If I comment out THIS ONE LINE OF CODE ... it works. If I leave it in ... it does not work.

     

    I need this code because we don't want the 100's of .NET properties to show up. We only want the 6 or so that are in the list defined by ddControl.Properties.

     

    Is there another way to achieve this requirement without the offending line of code?

    Friday, August 22, 2008 4:35 PM
  • I narrowed my binding list problem down to one line of code in the designer:

     

    Code Snippet

    For Each pd As PropertyDescriptor In TypeDescriptor.GetProperties(Me)

       Dim attr As BrowsableAttribute = TryCast(pd.Attributes(BrowsableAttribute.Default.GetType), BrowsableAttribute)

       Dim fld As FieldInfo

       fld = attr.GetType.GetField("browsable", BindingFlags.Instance Or BindingFlags.NonPublic)

       If ddControl.Properties.Contains(pd.Name) Then

          ' Modify the entry to turn on the Browsable attribute.

          fld.SetValue(attr, True)

       Else

          ' Modify the entry to turn off the Browsable attribute.

     ----> fld.SetValue(attr, False)

       End If

    Next

     

     

    If I comment out THIS ONE LINE OF CODE ... it works. If I leave it in ... it does not work.

     

    I need this code because we don't want the 100's of .NET properties to show up in the PropertiesGrid. We only want the 6 or so that are in the list defined by ddControl.Properties. (PostFilterProperties only works for the controls on the dialog, not the properties of the dialog itself.)

     

    Is there another way to achieve this requirement without the offending line of code?

     

    BRUCE from Microsoft: Can you confirm that this is a .NET bug?

    Friday, August 22, 2008 5:15 PM
  • I've GOT the solution! On your collection, implement the ITypedList interface. Remember in my example, the collection my DataGridView is bound to is a collection I created that derives from BindingList<MyClass>. So I implemented the ITypedList interface on that collection, and voila!

     

    I can't take credit though. See this for a reference:

    http://www.winterdom.com/weblog/2006/07/27/ITypedListAndPropertyDescriptors.aspx

     

    I hope this helps you!

     

    Friday, August 22, 2008 6:35 PM
  • I've GOT the solution! If you remember in my situation, the collection that is bound to my DataGridView is a collection I built that derives from BindingList<MyClass>. I simply implemented the ITypedList interface on this custom collection, and voila!

     

    I can't take credit for this, however. Here's where I got the idea:

    http://www.winterdom.com/weblog/2006/07/27/ITypedListAndPropertyDescriptors.aspx

     

    I hope this helps!

     

    Friday, August 22, 2008 6:37 PM
  • (Sorry for the double post. After my first post, I didn't see my post, so I re-posted.)

     

    Friday, August 22, 2008 6:38 PM
  • This article also helped lead me in the right direction ...

     

    http://dotnettemplar.net/CommentView,guid,15cbb182-5c44-4f6f-aad9-60e1cf238570.aspx

     

    Friday, August 22, 2008 7:11 PM
  • I finally got back to this problem. I implemented the ITypedList as per the link you provided. It did nothing to fix the problem. Do I need to modify the form code that does the binding to somehow use these methods of the ITypedList?

     

    Since you were so successful with this ... I assume I am just missing something.

     

    Thanks -

    Tuesday, September 16, 2008 4:36 PM
  • I know, that this is very old post, but maybe will still help someone in the future.

    The core problem is that - I'm guessing - you're binding an initially empty list to the DataGridView. This causes the DGV not to bind columns to properties properly, because - I'm guessing - it does it on instance of an item instead on its type (which could be retrieved using reflection, because data source most likely implements IList<T>, so there's a way to retrieve the "T", but DataGridView apparently doesn't attempt to do that).

    So the solution is to implement ITypedList by your list model, because from what I've seen, this interface aims to provide information about list's item's properties and that's what DGV isn't apparently able to retrieve by itself.

    An educated guess, but I think this is the case.

    Thursday, November 7, 2019 11:55 AM