✅ BindingList<T> ???
Context:
Hello, I'm working through my first C# class (which happens to be at the end of my degree program) and I've been recommended by my project instructions to use a binding list. The problem comes with my class having relatively poor material so I've learned just about everything through a Udemy course. The course didn't specifically cover .NET Framework / Window Form apps spefically so I'm having a couple hiccups in that aspect. I read through Microsoft's docs on BindingList but still don't really grasp what it does / it's use case. Additonally, I haven't been able to find anything other than AI content on YouTube.
I'll be using BindingList to implement several items that will be compiled into an inventory system being displayed in the form of a datatable.
Question: Would anyone be able to hop in a call and explain the basics of BindingList?
51 Replies
well your context doesn't really tell us much about what your app have, bindinglist is used in specific scenarios
BindingList is often used with say ListBox or DataGrid or ListView to provide an easy binding between the code behind and data present and automating notifications to the UI
so when you insert an item to your binding list it updates your actual list in the application
without the need of manually doing things around
whether u should or not use a bindinglist as opposed to say a datatable it will vastly depend on your app needs as well
I updated to answer my implementation as best as I can. I've been given my overall class structure and it seems like it's to have several properties as I believe they will all need to be updated together in a single datatable wihtin a given form.
are you using Ado?
or an ORM
This is my first time working with A: multi-form solutions and B: with updating any form element more complex than a label. The Udemy course I've been following has been focusing mainly on specific things (i.e. properties vs fields, sealed classes, ect) but it's been explaining everything through a console app.
I misread datatable as database 😛
so
normally
imagine you have a DataGrid
I'm not familar with what Ado / ORM are, however I don't believe so. This is a pretty bare bones solution all things considered.
you would use an adapter to populate the datatable
and use that to set the datagrid datasource
so are you not using an actual database? MSSQL, PostgreSQL etc?
I don't have a data grid. I have no idea how to make a datatable using windows forms but I was gonna figure that out when I got there lmao. I was getting pretty overwhelemed. For now I have a TableLayoutPanel with Panels inside but I have no idea if it'll work. I was gonna try and get most of my classes working before updating any forms.
I might be calling an API but nothing more than that. I haven't easily been able to find any API they want me to call yet, but I also havent gotten to the step of implementing it.
yeah I am pretty lost on what u have and what you're trying to do then since u have not provided anything about your app or your code
But I wont be usiung anything like MSSQL, no.
but sure imagine I have a ListBox or ListView I can call an API, fill a BindingList<T> and that will auto update my ListBox/ListView
Pretty much I have a few pictures showing what they want the GUI it look like, and I have a class structure that tells me what names and types proporties and methods are supposed to be. The diagram also shows how the classes are relient on each other. I think this is why I'm so lost on what a BindingList is / why to use it. I didn't come up with deciding for this proporty to be a BindingList I was just told to do so.
there is an example here using a ListBox https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.bindinglist-1?view=net-8.0
A ListBox is just a form element right?
yeah
I just checked
yes listbox is a winform element that lets u add multiple items to it
Sweet
can u provide some of the images of what the app is supposed to look like?
Is a BindingList essentially jsut a List that updates an element when an item inside of it is changed?
yeah
the good part of the BindingList is how easy it allows you to bind business logic to update the ui without much complexity
that looks more like a DataGrid
And this is my super rough not formatted idea I've made:
nah parts and products are just a DataGrid
so u would use a datatable instead
What element do I use to make datatables with?
DataGrid is the winform control
Currently I'm using a TableLayouPanel with Panels inside but I doubt thats the best way
DataTable is the type that u populate its datasource althou u might be able to use a BindingList too never tried
no that is not
they serve to a different purpose and they are not as complex as a DataGrid is
figured lmao Youtubeing "how to make a datatable in windows form" didn't get me anywhere
that adds up to why it was so hard to get it to look even the way it does in my second pic
Bind data to DataGridView Control - Windows Forms .NET Framework
Learn how DataGridView control supports the standard Windows Forms data binding model so it can bind to a variety of data sources.
is DataGridVeiw the same thing as DataGrid here?
yeah sorry many years not using winforms, DataGridView
all good
the only difference from the example in the link above
and I have zero expierence using DataBinding but I know there is a ton of videos on that I can check out, kept popping up when I was searching for BindingList
is that instead of using dataAdapter and Sql u will populate the DataTable yourself
since it comes from an API
So since I'm not using an actual database I have enter manually either way and this is the method to do so
If I'm understanding it correctly, BindingList is essentially a List that I'm able to use binding data with and bind said data to my DataGrid?
here is an example of the first link I sent u but instead of a listbox it uses a bindinglist https://learn.microsoft.com/en-us/answers/questions/1345418/datagridview-bindinglist-propertychanged
yes
BindingList is responsible for notifying the ui of changes done to the objects inside and the list it self
and viceversa
so if u add an item
to the bindlist it shows on the datagrid
if u modify that item on the datagrid
it updates the object in the bindinglist
is it kinda like having an event built into a list object?
sure u could say that
maybe not under the hood but logically functions in that sort of way
I mean that is what it does under the hood its just very complex bloat of reflection and what not
I think I've got enough to get some serious work done in this project. I'll go ahead and mark this as resolved. Is it cool if I send you a DM if I run something else I can't figure out or should I just make a new post here?
I dont take dms
Also how would I go about marking this as resolved
if u need help open a new help thread
$closer
All good
$close
If you have no further questions, please use /close to mark the forum thread as answered
pretty much all help happens in either one of the help channels
or here as a thread
I really appreciate the help, thank you!
Awesome! Makes sense to me