Binding DatagridView to List Provides "Extra" columns at runtime - vb.net

I have a list of objects called "TournamentPlayers" (derived from a class "PlayerInfo") in my project.
I have applied the following code to bind the lstTournPlayers to a DataGridView:
' Bind List of TournamentPlayers to Datagridview
Dim bindingList = New BindingList(Of TournamentPlayers)(lstTournPlayers)
Dim source = New BindingSource(bindingList, Nothing)
dgvPlayers.DataSource = source
dgvMembershipNo.DataPropertyName = "strMembershipNo"
dgvPlayerName.DataPropertyName = "strMembershipName"
Found from an answer here on SO, but repurposed: https://stackoverflow.com/a/16695971/692250
However, I noticed that after setting up the columns within the datagrid view, I get this weird column heading now:
My question is: how can I bind the parts of the list to this datagridview.
For anyone interested here is the schema of the list:
Public Class PlayerInfo
Public strPlayerFirstName As String
Public strPlayerLastName As String
Public strMembershipNo As String
Public strMembershipName As String
End Class
Public Class TournamentPlayers : Inherits PlayerInfo
Public intStatus As Integer = 0
Public intByeRounds As Integer
Public intTeamID As Integer
End Class

Related

Code to for AutoMapper complex mapping

AutoMapper with VB.NET
I have the following classes below. OrderA With List (Of OrderALineItem) and OrderBList With List (Of OrderB). I want to copy data from OrderA to OrderBList. Which copies ItemName, ItemQty, Price from List (Of OrderALineItem) to List (Of OrderB) and OrderID, CustomerName from OrderA itself. I have found almost all codes in C# and am not able to convert it to vb.net code.
Public Class OrderA
Public Property OrderID As String
Public Property CustomerName As String
Public Property OrderLineItem As List(Of OrderALineItem)
End Class
Public Class OrderALineItem
Public Property ItemName As String
Public Property ItemQty As Integer
Public Property Price As Decimal
End Class
Public Class OrderBList
Public Property OrderBLineItem As List(Of OrderB)
End Class
Public Class OrderB
Public Property OrderID As String
Public Property CustomerName As String
Public Property ItemName As String
Public Property ItemQty As Integer
Public Property Price As Decimal
End Class
My VB.NET code until now is:
Dim mapperConfiguration = New MapperConfiguration(Sub(config)
config.CreateMap(Of OrderALineItem, OrderBList)()
End Sub)
Dim mapper = mapperConfiguration.CreateMapper()
Dim objOrderB = mapper.Map(Of OrderBList)(objOrder.OrderLineItem)
The above code creates and object from copies the data from objOrder.OrderLineItem to OrderBList. That's it.
Can anybody help me out on this in VB.NET.
Note: Am totally new in AutoMapper
Version: AutoMapper 6.2.2.0
Done myself, I hope the code below will be helpful to somebody.
Dim mapperConfiguration = New MapperConfiguration(Sub(config)
config.AddProfile(New CustomProfile_1)
End Sub)
Dim objMapper = mapperConfiguration.CreateMapper()
Dim objOrderB As List(Of Dest_OrderB) = objMapper.Map(Of Src_OrderA, List(Of Dest_OrderB))(objOrderA)
Public Class CustomProfile_1
Inherits Profile
Sub New()
CreateMap(Of Src_OrderALineItem, Dest_OrderB)()
CreateMap(Of Src_OrderA, List(Of Dest_OrderB))() _
.ConstructProjectionUsing(
Function(Src1) Src1.List_Src_OrderALineItem.Select(Function(Src2) New Dest_OrderB _
With {.CustomerName = Src1.CustomerName,
.OrderID = Src1.OrderID,
.ItemName = Src2.ItemName,
.ItemQty = Src2.ItemQty,
.Price = Src2.Price}
).ToList())
End Sub
End Class

Get value of a property with propertyinfo object

Is there a way to get value of a object properties with a propertyinfo object?
psudo code:
propertyinfoObject = Text
myobject.toCommand(propertyinfoObject)
The psudo code above should do the same as
myobject.Text
My goal is to create a simpel Properties form that will work on any object (Later I will use keywords to filter out what options I want the use to see).
My real code
Public Class PropertiesForm
Dim propertyInfoVar() As PropertyInfo
Dim Properties As New Form2
Dim listItem As New ListViewItem
Dim stringarray() As String
Public Sub New(ByRef sender As Object)
propertyInfoVar = sender.GetType().GetProperties()
For Each p In propertyInfoVar
stringarray = {p.Name.ToString, #INSERT VALUE SOMEHOW HERE#}
listItem = New ListViewItem(stringarray)
Properties.ListView1.Items.Add(listItem)
Next
Properties.Visible = True
End Sub
EDIT
Just use propertyGrid as suggested below!
The standard PropertyGrid already does all that for you. Filtering properties is not so obvious, here's how:
The control includes a BrowsableAttributes property which allows you to specify that only properties with the specified attribute value should be shown. You can use existing attributes, or custom ones. This is specifically for tagging visible props:
<AttributeUsage(AttributeTargets.Property)>
Public Class PropertyGridBrowsableAttribute
Inherits Attribute
Public Property Browsable As Boolean
Public Sub New(b As Boolean)
Browsable = b
End Sub
End Class
Apply it to an Employee class to hide pay rates or anything else:
Public Class Employee
<PropertyGridBrowsable(True)>
Public Property FirstName As String
...
<PropertyGridBrowsable(False)>
Public Property PayRate As Decimal
<PropertyGridBrowsable(False)>
Public Property NationalInsuranceNumber As String
Test code:
Dim emp As New Employee With {.Dept = EmpDept.Manager,
.FirstName = "Ziggy",
.PayRate = 568.98D,
...
.NationalInsuranceNumber = "1234567"
}
propGrid.BrowsableAttributes = New AttributeCollection(New PropertyGridBrowsableAttribute(True))
propGrid.SelectedObject = emp
BrowsableAttributes is a collection, so you can add several.

PropertyInfo is coming as nothing in vb.net

I am using this code snippet in vb.net . The propertyinfo p is coming out as nothing even though my column name of dataTable is matching with class name attributes
Public Class ReflectionMethods(Of T As New)
' function that set the given object from the given data row
Public Shared Sub SetItemFromRow(item As T, row As DataRow)
' go through each column
For Each c As DataColumn In row.Table.Columns
' find the property for the column. at this point i am getting p as nothing
Dim p As PropertyInfo = item.GetType().GetProperty(c.ColumnName)
' if exists, set the value
If p IsNot Nothing AndAlso row(c) IsNot DBNull.Value Then
p.SetValue(item, row(c), Nothing)
End If
Next
End Sub
End Class
Final end result that I am getting is a class object with everything set to nothing as it is not passing the if condition.
Hi Jon, I have pasted my class snippet down
Public Class StockProduct
Public SupplierName As String
Public ModelName As String
Public ModelDescription As String
Public ProductCategoryName As String
Public ManufacturerName As String
End Class
and I have a dataTable with column match
. Please note the productcategoryName is matched but not seen in screenshot
Your class doesn't have any properties. The line
Public SupplierName As String
creates a field, not a property.
To fix your code do one of the following...
either change the class declaration to say
Public Property SupplierName As String
etc...
or change your property reading code to say
Dim p As FieldInfo = item.GetType().GetField(c.ColumnName)
Options:
ColumnName is wrong - doesn't exactly match the name of the property.
There is no public property with the name you're expecting. Maybe its a Field or a Private property.
Maybe update your question with an example of the ColumnName from the DataTable, and also the definition of a class that you're attempting to populate.

Returning Class Object from Inherited class

I'm trying to teach myself reflection and have been googling but I can't wrap my head around it entirely. I created a class called DataClass which contains a method called GetClassFromDB as you can see below, which will be inherited from multiple classes.
What I am attempting to do is have my dataclass read the TableName property that is defined within objResults. Once I pull in the tablename from objResults I would query the SQL database for a dataset. Once I have the dataset I would create a new object of the same TYPE inheriting this class (Which will be different types) and populate it from the dataset. Once I have the newly populated class I will return it for use.
I believe I have gotten most of the way there properly (Please correct me if there is a better way), but my real question is this. How can I create a new class of the type thats deriving that class from that string name that I getting in my code, or the type. I would want to have all the accessible properties from objResults available.
Namespace MyApp
Public Class DataClass
Private _TableName As String
Private _Name As String
Overridable ReadOnly Property TableName As String
Get
Return _TableName
End Get
End Property
Public Overloads Function GetClassFromDB() As Object
Try
Dim BaseObject As New Object
'Get the object name
Dim objName As String = MyBase.GetType().Name
'Gets the type thats calling this method
Dim objDerived As Type = MyBase.GetType()
'Get the property info to request the tablename from the derived class
Dim TableName As PropertyInfo = objDerived.GetProperty("TableName")
Dim TableNameString As String = TableName.GetValue(Me, Nothing).ToString
'Once I get the table name from objResults I can perform the SQL
Dim QueryResults as DataSet = SQLiteCLass.Query("Select * FROM TableNameString")
'Once I get data from the SQL I want to create a new object of the type deriving this method.
'In this example is objResults
Dim NewObject as objDerived
'Now I can fill my new object with the results and return it as an object
'THIS IS MY QUESTION - How can I create a new object of the TYPE that I receive from Reflection
Return False
Catch ex As Exception
Return False
End Try
End Function
End Class
End Namespace
and this is a sample class that would inherit my dataclass.
Public Class objResults
Inherits MyApp.DataClass
Private _GameID As Guid
Public Property GameID As Guid
Get
Return _GameID
End Get
Set(ByVal value As Guid)
_GameID = value
End Set
End Property
Public Overrides ReadOnly Property TableName As String
Get
Return "This is my tablename"
End Get
End Property
End Class
and this is how I would use this in code.
Dim objResult as New objResults
Dim TodaysResult as objResultsCollection
TodaysResult = objResult.GetClassFromDB()

Unable to cast custom listviewitem class in vb.net

This is a followup question to another question I asked earlier. I thought I had everything I needed, but I'm running into another issue. I'm trying to use a custom listviewitem class that attaches additional information to a lisview item. Here is the class:
Public Class albumListViewItem
Inherits ListViewItem
Public hash As String
Public id As Integer
Public provider As String
Public providerID As String
Public providerURL As String
Public providerArtistID As String
Public albumName As String
Public albumType As String
Public numTracks As Integer
Public imageURLs() As String
Public genres() As String
Public styles() As String
Public label As String
Public year As String
Public country As String
Public rating As String
Public editorsPick As Boolean
Public sampleStreamURL As String
Public providerReview As String
End Class
When I try to cast a listviewitem to my custom class like this:
Dim albumItem As albumListViewItem = CType(lsvHidden.items.item(0), albumListViewItem)
I get the following error, "Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'AudioMatic.albumListViewItem'."
What am I missing here?
From your previous question and this one, I think a better fit for your problem would be to use a regular ListViewItem and store the accessory information in ListViewItem.Tag
You can do
Dim listViewItem As New ListViewItem("SomeText")
Dim albumInfo As New albumListViewItem()
albumInfo.albumName = "SomeAlbum"
...
listViewItem.Tag = albumInfo
listView1.Items.Add(listViewItem)
and then retrieve it like this
Dim selectedItem As ListViewItem = listView1.SelectedItems(0).Item
Dim alubmInfo As albumListViewItem = TryCast(selectedItem.Tag, alubmListViewItem)
Dim albumName as String = albumInfo.albumName
see if this solution will work for you.
If you step through the code and watch the variable "lsvHidden.items.item(0)" you should be able to first tell if it is in fact of type "albumListViewItem" or something else. Are you sure it was albumListViewItem that was added to the list in the first place?
Some alternatives to what you are doing;
1. You can implement an object and store it in the tag of the ListViewItem.
2. The following article seems to describe another approach of adding Columns to the listview to allow storing extra information on the listview itself; http://www.codeproject.com/KB/list/ListViewExtendedItem.aspx
I can appreciate your situation as I would have expected that to work. And I can see advantages and reasons for doing it that way as well. Not sure if the code project sample is adaptable to what you need, so you'll need to review the concept.
The working code:
Public Class albumListViewItem
Inherits ListViewItem
Public hash As String
Public id As Integer
Public provider As String
Public providerID As String
Public providerURL As String
Public providerArtistID As String
Public albumName As String
Public albumType As String
Public numTracks As Integer
Public imageURLs() As String
Public genres() As String
Public styles() As String
Public label As String
Public year As String
Public country As String
Public rating As String
Public editorsPick As Boolean
Public sampleStreamURL As String
Public providerReview As String
End Class
Storing information using listviewitem.tag:
Dim listViewItem As New ListViewItem("SomeText")
Dim albumItem As New albumListViewItem
albumItem.albumName = "Test Album"
albumItem.id = "testid"
albumItem.Text = albumItem.albumName
albumItem.year = "2011"
albumItem.numTracks = 10
'....
listViewItem.Tag = albumItem
'add viewable items to listview
albumItem.SubItems.Add(albumItem.year)
albumItem.SubItems.Add(albumItem.numTracks)
'....
ListView1.Items.Add(albumItem)
Reading the information that was previously stored:
Dim albumInfo As albumListViewItem = CType(ListView1.SelectedItems(0), albumListViewItem)
Dim id as string = alumInfo.id