How to Create Report Viewer Binding to Object in VB.NET - vb.net

I have Class named "User", code:
Public Class User
Private m_id As String
Private m_user_name As String
Public Sub New(ByVal id As String, ByVal name As String, ByVal pwd As String)
m_id = id
m_user_name = name
m_pwd = pwd
End Sub
Public Property Id() As String
Get
Return m_id
End Get
Set(ByVal value As String)
m_id = value
End Set
End Property
Public Property Name() As String
Get
Return m_user_name
End Get
Set(ByVal value As String)
m_user_name = value
End Set
End Property
End Class
And I want to report all the user's information, so I also have another class named "Users", code:
Public Class Users
inherits List(Of User)
Public Sub New()
'Query users' information from Table in Database
Add(New User(...)
End Sub
End Class
When I clicked on "Print" button, it will pop up the report. I have only one Form with Report Viewer, but I have a lot of reports using this Form with Report Viewer. so I code like this:
Public Class Form1
Private Sub Form1_Load(...) Handles MyBase.Load
Dim bs As New BindingSource
bs.DataSource= New Users
Dim rpt As New Microsoft.Reporting.WinForms.ReportDataSource
rpt.Name = "Report_User"
rpt.Value = bs
ReportViewer1.LocalReport.DataSources.Add(rpt)
ReportViewer1.LocalReport.ReportEmbeddedResource = rpt.Name
ReportViewer1.RefreshReport()
End Sub
But it doesn't show anything... Please help me... I really need your help..
Thanks in advance

Now I can resolve my problem.. Click here for reference

Related

vb.net - Can't acces custom Form property/method from another Form

I have a vb.net form with this code:
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing.Printing
Public Class frmTransAlqPago
Private dt As DataTable
Private da As OleDbDataAdapter
Private BaseDatos As String
Private cProp As cPropietario
Private cAlqu As cAlquiler
Private cClie As cCliente
Private cPrda As cPropiedad
#Region "Propiedades"
Private mvarIDAlqu As Long
Private mvarIDClie As Long
Private mvarIDProp As Long
Private mvarIDPrda As Long
Private mvarFecDeu As Date
Public WriteOnly Property pIDAlqu() As Long
Set(ByVal value As Long)
mvarIDAlqu = value
End Set
End Property
Public WriteOnly Property pIDClie() As Long
Set(ByVal value As Long)
mvarIDClie = value
End Set
End Property
Public WriteOnly Property pIDProp() As Long
Set(ByVal value As Long)
mvarIDProp = value
End Set
End Property
Public WriteOnly Property pIDPrda() As Date
Set(ByVal value As Date)
mvarpIDPrda = value
End Set
End Property
Public WriteOnly Property pFecDeu() As Long
Set(ByVal value As Long)
mvarFecDeu = value
End Set
End Property
#End Region
Private Sub mObtieneObjetos()
cClie.mClienteGet(CType(txtIDClie.Text, Integer))
cAlqu.mAlqPagoGet(CType(txtIDAlqu.Text, Integer))
cPrda.mPropGet(CType(txtIDPrda.Text, Integer))
cProp.mGetPropietario(CType(txtIDProp.Text, Integer))
End Sub
...
...
...
On another Form I have:
Private Sub btnImprimir_Click(sender As Object, e As EventArgs) Handles btnImprimir.Click
Dim frm As Form = New frmTransAlqPago
frm.Controls.Item("txtIDAlqu").Text = CType(dgvVarios.CurrentRow.Cells.Item(4).Value, Integer)
frm.Controls.Item("txtIDPrda").Text = CType(dgvVarios.CurrentRow.Cells.Item(5).Value, Integer)
frm.Controls.Item("txtIDClie").Text = CType(dgvVarios.CurrentRow.Cells.Item(6).Value, Integer)
frm.Controls.Item("txtIDProp").Text = CType(dgvVarios.CurrentRow.Cells.Item(7).Value, Integer)
frm.Controls.Item("txtAlqRec").Text = CType(dgvVarios.CurrentRow.Cells.Item(0).Value, Integer)
frm.pfecdeu = CType(dgvVarios.CurrentRow.Cells.Item(0).Value, Integer)
The last instruction is given me an error. In fact autocomplete function doesn't recognize frm.pFecDeu
So how can I acces custom property/method from one Form into another one? I'm writing code in Visaul Studio 2015
Thank
I think your issue is Dim frm As Form = New frmTransAlqPago.
And my solution is Dim frm As frmTransAlqPago = New frmTransAlqPago.

Add rows to class bound datagridview

Hi I'm trying to bind a list of objects to a datagridview
Binding a existing list(Of is working but I'm trying to add or remove a object from, my dgv isn't updating.
Public Class Form1
Dim lt As New List(Of Test)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lt.Add(New Test("Mac", 2200))
lt.Add(New Test("PC", 1100))
dgv.DataSource = lt
lt.Add(New Test("Android", 3300)) 'This line won't appear in the dgv
End Sub
End Class
Public Class Test
Public Sub New(ByVal name As String, ByVal cost As String)
_name = name
_cost = cost
End Sub
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _cost As String
Public Property Cost() As String
Get
Return _cost
End Get
Set(ByVal value As String)
_cost = value
End Set
End Property
End Class
How can I add or remove or change a value from the dgv to the list and inverse?
NoiseBe
Change this line:
Dim lt As New List(Of Test)
To:
Imports System.ComponentModel
...
Private lt As New BindingList(Of Test)
When the collection contents will change, you should use a BindingList(Of T). This collection has events associated with it which make it aware of changes to the list contents.
If in addition to the list contents, the list items will change (like Test.Name), you should also implement INotifyPropertyChanged on the class itself.
Another way to do it:
dgv.DataSource = Nothing
lt.Add(New Test("Android", 3300))
dgv.DataSource = lt
This "resets" the DataSource so that the new contents will show up. However, it means that several other things get reset like selected items; if you are binding to a List/Combo control, you will also have to reset the ValueMember and DisplayMember properties as well.

Removing an object from List (Of T)

I'm not able to remove an object from my List (of contact)
Here are my fields:
Public Class Contact
'Things to remember
Private m_firstName As String = String.Empty
Private m_lastName As String = String.Empty
Private m_address As Address
My list:
Public Class ContactManager
Private m_contactRegistry As List(Of Contact)
Public Sub New()
m_contactRegistry = New List(Of Contact)()
End Sub
My method in ContactManger Class. Here I'm getting error "Value of type 'Integer' cannot be converted to Assignment.Contact" on the index
Public Function DeleteContact(index As Integer) As Boolean
m_contactRegistry.Remove(index)
Return True
End Function
My delete button method on my Main class:
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
'listResults is my listbox
Dim list = listResults.SelectedIndex
'm_contact is an object of the Contact class
m_contacts.DeleteContact(list)
UpdateGUI()
End Sub
The problem is that I don't know how to do the method DeleteContact(index As Integer) without getting an error. Do you guys have a suggestion?
When using an index, you need RemoveAt() rather than Remove()

Binding Combobox to Object Datasource

I've got a form bound to an object datasource. It has one text box and one combo box. I set up one binding source for the main object and one binding source for the combo box. When I run the form, the text box is bound correctly, and the list of values in the combo box is bound correctly, but the ValueMember of the combo box isn't working correctly.
The combo box shows the correct list, but it's selected index is 0 instead of what it should be 2. When I change the value in the text box, it's bound object's Property.Set method is called correctly, but the same Property.Set method is not called for the combo box.
I know I can hack up the OnSelectedIndex change methods in the form, but I would like to know what I am doing wrong in just using the Bindings.
Here is the code on the form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim NameValueBindingSource1 As New BindingSource()
Dim WorkOrderBindingSource1 As New BindingSource
'Create main object to bind to
Dim wo As New WorkOrder
wo.WOIndex = "2012-0111"
wo.WorkOrderType = 3
'Create list object for combo box
Dim NameValues As BindingList(Of NameValue)
NameValues = FillNameValueList()
'Bind Text Box to Binding Source
WorkOrderBindingSource1.DataSource = wo
WOIndexTextBox1.DataBindings.Add("Text", WorkOrderBindingSource1, "WOIndex")
'Bind Combo Box to Binding Source
NameValueBindingSource1.DataSource = NameValues
WorkOrderTypeCombo.DataSource = NameValueBindingSource1
WorkOrderTypeCombo.DisplayMember = "Value"
WorkOrderTypeCombo.ValueMember = "Code"
End Sub
Function FillNameValueList() As BindingList(Of NameValue)
Dim bl As New BindingList(Of NameValue)
Dim nv As NameValue
nv = New NameValue
bl.Add(New NameValue("Short", 0))
bl.Add(New NameValue("Middle", 1))
bl.Add(New NameValue("Long", 2))
bl.Add(New NameValue("Very Long", 3))
Return bl
End Function
End Class
Here's the code for the main object - "WorkOrder"
Imports System.ComponentModel
Public Class WorkOrder
Implements IEditableObject
Implements INotifyPropertyChanged
Private mWOIndex As String
Private mWorkOrderType As Integer
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Property WOIndex As String
Get
Return mWOIndex
End Get
Set(value As String)
mWOIndex = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("WOIndex"))
End Set
End Property
Public Property WorkOrderType As Integer
Get
Return mWorkOrderType
End Get
Set(value As Integer)
mWorkOrderType = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("WorkOrderType"))
End Set
End Property
Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
End Sub
Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
End Sub
Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
End Sub
End Class
Here's the code for the object used in the combo box
Imports System.ComponentModel
Public Class NameValue
Implements IEditableObject
Implements INotifyPropertyChanged
Private mValue As String
Private mCode As Integer
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Property Code As Integer
Get
Return mCode
End Get
Set(value As Integer)
mCode = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Code"))
End Set
End Property
Public Property Value As String
Get
Return mValue
End Get
Set(value As String)
mValue = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Value"))
End Set
End Property
Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
End Sub
Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
End Sub
Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
End Sub
Public Sub New(InitValue As String, InitCode As Integer)
Value = InitValue
Code = InitCode
End Sub
End Class
In your code, you are merely assigning the DataSource to the ComboBox, but you're not establishing any DataBinding for it.
You need a line like this (using C# here):
WorkOrderTypeCombo.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", WorkOrderBindingSource1, "WorkOrderType", true));
Hope this helps

How can I make a list in a Repeater Section?

I need to create a repeater section that will show 4 columns - First Name, Last Name, a link based off of stored column data that says.
All the data plus some extra not being used is in a players profile. How do I link the data on the code-behind to the repeater control with the databinders?
I am using visual studio 2008, VB.NET for the code behind.
Have you considered using a DataGrid instead of a repeater?
Here's a bit of a breakdown on when to use each.
http://msdn.microsoft.com/en-us/library/aa479015.aspx
To more directly answer your question you'll need to set the Repeater's DataSource property to a DataView or an ArrayList. As such:
Sub Page_Load(Sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim values As New ArrayList()
values.Add(New PositionData("Microsoft", "Msft"))
values.Add(New PositionData("Intel", "Intc"))
values.Add(New PositionData("Dell", "Dell"))
Repeater1.DataSource = values
Repeater1.DataBind()
Repeater2.DataSource = values
Repeater2.DataBind()
End If
End Sub
Public Class PositionData
Private myName As String
Private myTicker As String
Public Sub New(newName As String, newTicker As String)
Me.myName = newName
Me.myTicker = newTicker
End Sub
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
Public ReadOnly Property Ticker() As String
Get
Return myTicker
End Get
End Property
End Class