• 0

Binding a DataGridView to an IList


Question

Note: While this sample is in VB.NET, I am language agnostic when it comes to a solution (i.e. I'll either code in back up in C# or convert a C# solution)

Setup:

1. Class with three properties, Firstname, Lastname, Age

2. Create 5 instances and add then to a List(of Class)

3. Set the List as the datasource for a DataGridView(setup as read only)

4. By default, the datagridview displays three columns and five rows

Question, is there a way to mark (via an attribute for example) the Age property in TestClass so it does not automatically bind/display in the grid?

   
   Dim foo As New List(Of TestClass)

   For i As Integer = 1 To 5
	 foo.Add(New TestClass("FirstTest " & i.ToString(), "LastTest " & i.ToString(), i))
   Next

   Me.DataGridView1.DataSource = foo

I thought I could do it by marking the age property as

   <System.ComponentModel.Bindable(False)> _
   Public Property Age() As Integer
	 Get
	   Return _age
	 End Get
	 Set(ByVal value As Integer)
	   _age = value
	 End Set
   End Property

But that didn't work, it still gets bound (at least visually)

Link to comment
https://www.neowin.net/forum/topic/599628-binding-a-datagridview-to-an-ilist/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

You need to set up a Binding source, and then bind the list to the binding source and then the datagridview to the binding source:

Once you do this, you can then manipulate the datagridview using the IDE to show and hide those columns that you want to display, and which columns of the binding source you want to bind to what when displaying them

  • 0
You need to set up a Binding source, and then bind the list to the binding source and then the datagridview to the binding source:

Once you do this, you can then manipulate the datagridview using the IDE to show and hide those columns that you want to display, and which columns of the binding source you want to bind to what when displaying them

Thats what I am trying to avoid. I would prefer to have the data dictate what is visible if possible. By the way, the List will bind to the grid just fine without overhead of the BindingSource object.

  • 0

The DataGridView control contains the property 'AutoGenerateColumns', which will cause the control to generate a column for each public property and/or field. Set this to false, but you will need to manually create your columns and binding settings.

  • 0

Antaris, that is what I did (in a way). I landed up using a custom attribute to mark each property then created a re-usable grid fill routine that would only add the data to a grid that was marked 'bindable'. I was just looking for a shortcut...

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • AB Download Manager 1.9.1 by Razvan Serea AB Download Manager is an open-source, feature-rich download manager designed to accelerate downloads, organize files efficiently, and provide seamless control over downloads. With support for multiple connections, resume capability, and an intuitive interface, it enhances the downloading experience for users seeking speed and reliability. The software integrates with various browsers, enabling quick link grabbing and batch downloading. It supports HTTP, HTTPS, and FTP protocols, ensuring broad compatibility with different file sources. Users can schedule downloads, set speed limits, and categorize files automatically for better organization. AB Download Manager is lightweight yet powerful, making it a great alternative to proprietary download managers. Its open-source nature allows developers to contribute, customize, and improve the software as needed. Whether you're downloading large files, managing multiple downloads at once, or seeking an ad-free experience, this tool offers a practical and efficient solution. Key features of AB Download Manager: Multi-Connection Support – Accelerates downloads by splitting files into multiple segments. Resume Capability – Allows paused or interrupted downloads to be resumed without starting over. Batch Downloading – Supports downloading multiple files at once for improved efficiency. Browser Integration – Captures download links directly from browsers for seamless operation. HTTP, HTTPS, and FTP Support – Ensures compatibility with a wide range of file sources. Download Scheduling – Enables users to automate downloads at specific times. Speed Limiting – Lets users control bandwidth usage for optimized performance. File Categorization – Automatically organizes downloaded files into designated folders. User-Friendly Interface – Simple and intuitive design for easy navigation. Cross-Platform Compatibility – Works on multiple operating systems. Ad-Free Experience – No intrusive ads or tracking for a clean user experience. AB Download Manager 1.9.1 changelog: Added An option to customize notification sounds (#1259) Fixed Ongoing notification was laggy on Samsung One UI devices (#1269) Improved Updated Translations Minor UI/UX improvements Download: AB Download Manager 1.9.1 | Portable | ~80.0 MB (Open Source) Download: ARM64 | Portable ARM64 | Android Links: AB Download Manager Website | Github Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • watching him because of the Mr Klinton cat
    • yup dude, ADS on this website are terrible
    • Samsung Galaxy Z Fold 8, Flip 8, Z Fold Wide: Everything you need to know The ONLY thing I need to know is the price, which I know will be way higher than I (and most people) are willing to pay for a phone... so basically nothing here I need to know. PS: Nice job getting that Apple reference to a non-existent and unrevealed product as "competition" in there. Cheque is in the mail.
  • Recent Achievements

    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
    • One Month Later
      agatameier earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      505
    2. 2
      +Edouard
      196
    3. 3
      PsYcHoKiLLa
      140
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!