I'm just trying to figure out how to use Report Viewer in VB.NET.
The report has only one text box with the data element name set to ReportName.
The code is simple.
Private Sub frmCalibrationPreviewReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If _CalibrationReportID <> -1 Then
With rvCalibrationReport
.LocalReport.DataSources.Clear()
.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
Dim tmpData As DataTable = modDeclare.SelectSQL("SELECT ReportName FROM tblReportTypes")
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData))
End With
End If
Me.rvCalibrationReport.RefreshReport()
End Sub
Nothing is appearing on the report, it should contain two records.
Where am I going wrong?
Jim
Here's a great article covering Report Viewer
I mention this link as it appears your new to this. I would recommend reading this first.
Try Changing
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData))
To
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData.defaultview))
I would also suggest looking at this question, very similar to yours...
Bind DataTable to RDLC and ReportViewer
The problem with this question is there are a lot of gears at work when using reporting in visual studio. The problem could also be in the report file itself (.RDLC) if the field is not referencing the data source correctly. With the limited amount of information all I could suggest is using a working template and slowly adding your desired elements one at a time.
Related
I don't normally post on forums because I try to find information for myself, and ask as an absolute last resort. I've tried scouring the net for answers, but I'm only receiving about half of the answer I'm looking for.
I'm currently building an application that deals with state law. There's one combo box and one text box. One for the offense title, and one for the numerical code for that particular code section. So say if I select "Kidnapping", it prepopulates the text box below it with "11-5-77", for example.
The method I've been using for, oh, about the last hour now, is:
If AWOffenseTitle.Text = "Kidnapping" Then
AWCN.Text = "11-5-77"
ElseIf AWOffenseTitle.Text = "False Imprisonment" Then
AWCN.Text = "11-5-78"
With AWOffenseTitle being the combo box name, and AWCN being the text box name. While this has proved to work perfectly well so far, I'm sure you can imagine with hundreds of offense titles, this is going to take a ridiculously long time. Well, I finally found a spreadsheet with offense titles and their respective title codes. What I'm looking to do is create two text files within a folder in the local directory "Offenses". One with a vertical list of offenses, and one with a vertical list of offense code numbers that populate the same lines in each. What I'm looking to do is populate the combo box with the contents of text file one (which I can do already), but then selecting an offense title will read the second text file and display it's proper title code. That's what has me at a loss. I'm relatively well-versed with vb.NET, but I'm not an expert by any means.
I'm hoping someone here will be able to provide a code example and explain it to me line-by-line so I can gain a better understanding. I want to get more proficient with VB although it's not so popular anymore. I've been using VB since 6.0, but not on a regular basis. More on a sporadic project kind of basis.
I really appreciate any assistance anyone might be able to provide, and if you need more information, I'd be glad to answer any questions. I tried to be as thorough as I could.
Thank you in advance!
First, you need to retrieve your data. I demonstrated using an Sql Server database containing a table named Offenses with columns named OffenseTitle and OffenseCode. You will have to change this code to match your situation.
Private Function GetOffenseData() As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection("Your connection string"),
cmd As New SqlCommand("Select OffenseTitle, OffenseCode From Offenses;")
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function
As the Form loads, set the properties of the ComboBox. DisplayMember matches the name of the title column and ValueMember is the name of the code column.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = GetOffenseData()
ComboBox1.DisplayMember = "OffenseTitle"
ComboBox1.ValueMember = "OffenseCode"
ComboBox1.DataSource = dt
End Sub
Then when the selected item in the combo changes, just set the .Text property of TextBox to the SelectedValue in the combo and your code appears.
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
TextBox1.Text = ComboBox1.SelectedValue.ToString
End Sub
There are other ways to do this if your data source is other than a database. Please advise if you need additional help.
In addition to HardCode's comment and Mary's detailed answer, I can only add an answer that's somewhere in between them.
It might be the case, that the information is not taken from a database, but from another source, like a text/data file or a web service. So it might be useful to create an abstraction for the data source you actually use.
First, I create a class or struct that will hold the data for each combo box item.
Class Offense
Public ReadOnly Property Title As String
Public ReadOnly Property Code As String
Public Sub New(title As String, code As String)
Me.Title = title
Me.Code = code
End Sub
End Class
Next, you need a method that retrieves a list of offenses that you can bind to your combo box. It's entirely up to you how you fill/fetch the offenses list. I have simply hard coded your two values here.
Private Function GetOffenseData() As List(Of Offense)
Dim offenses As New List(Of Offense)
offenses.Add(New Offense("Kidnapping", "11-5-77"))
offenses.Add(New Offense("False Imprisonment", "11-5-78"))
Return offenses
End Function
At a certain moment (probably in your form's Load event handler), you need to initialize your combo box. Just like Mary did, I use data binding.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AWOffenseTitle.DropDownStyle = ComboBoxStyle.DropDownList
AWCN.ReadOnly = True
AWOffenseTitle.DisplayMember = NameOf(Offense.Title)
AWOffenseTitle.ValueMember = NameOf(Offense.Code)
AWOffenseTitle.DataSource = GetOffenseData()
End Sub
Note that I use the NameOf operator to get the desired property names of the Offense class. If you ever decide to rename the properties of your Offense class, you will be able to easily detect where they are used, since the compiler will complain if your code still uses the wrong property names somewhere.
Finally, the app needs to react to combo box value changes, so that the text box will show the corresponding offense code. Mary used an event handler for the SelectionChangeCommitted event, but I use a handler for the SelectedIndexChanged event instead:
Private Sub AWOffenseTitle_SelectedIndexChanged(sender As Object, e As EventArgs) Handles AWOffenseTitle.SelectedIndexChanged
AWCN.Text = AWOffenseTitle.SelectedValue
End Sub
(Up to now, I was not aware of the SelectionChangeCommitted event of the ComboBox control. I will need to look into this event to see if it is actually a better choice for this scenario, but I found that the SelectedIndexChanged event does the job just fine, so for now I sticked with that event, since I am more familiar with it.)
So basically, I believe I am using the correct code yet the database will still not update. It will work for the current session, however, when I stop and restart the program, it appears that the data has not been updated in the database.
The really interesting part is that I am using the same method to update the database elsewhere, which when used and session restarted, the database has been updated.
p.s. I also have the same adapters and binding sources set up etc on both forms
I am so confused, help pls
Code that I believe is correct but is not working: (updating on another form so I have one place where all forms update hence FRMMain. etc)
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
Dim CurrentPoints As Integer
Dim UpdatedPoints As Integer
CurrentPoints = FRMMain.MyDBDataSet.Tables("TBLPupil").Rows(looopcount)(15)
UpdatedPoints = CurrentPoints + stfPoints
FRMMain.MyDBDataSet.Tables("TBLPupil").Rows(looopcount)(15) = UpdatedPoints
FRMMain.TBLPupilTableAdapter.Update(MyDBDataSet.TBLPupil)
FRMMain.TBLPupilTableAdapter.Fill(MyDBDataSet.TBLPupil)
End Sub
Code that I am using in another form that that DOES work:
Private Sub BtnYes_Click(sender As Object, e As EventArgs) Handles BtnYes.Click
Dim Points As Integer = FRMPupil.Pointss
Dim Cost As Integer = FRMPupil.RewardCost
Points = Points - Cost
FRMPupil.LePoints = Points
MyDBDataSet.Tables("TBLPupil").Rows(FRMLogin.DBLocation)(15) = Points
FRMMain.TBLPupilTableAdapter.Update(MyDBDataSet.TBLPupil)
FRMMain.TBLPupilTableAdapter.Fill(MyDBDataSet.TBLPupil)
Me.Hide()
End Sub
My code is correct but is not working.
No, if it is not working, then it is not correct!
There are different things you can do: DRY, Dont Repeat Yourself. You are repeating the code for updating points at several places in your code. This is error prone. Write it once and re-use it, e.g. by applying the the Repository Pattern. It makes it easier to detect errors and correct them. It allows you to re-use code that has already been tested in other scenarios (on another form).
Debug, debug, debug. Place breakpoints in the not working methods and see what happens. Do all the variables have the expected values? E.g., does looopcount have the same value as FRMLogin.DBLocation? There must be a difference somewhere. See: Navigating through Code with the Debugger or the more recent article Debug your Hello World application with Visual Studio 2017.
In a vb.net WinForms application, we have a RadGridView with filtering. We have a column of categories and some users will want to filter to see only rows with specific category. However, the only values represented in the filter popup are what are currently represented in the grid/datasource. Our users want to be able to select a category, even if it is not yet in the grid.
I have seen some mention of OnDistinctValueLoading as a solution online, but it seems I do not have that event on my RadGridView.
I think we're using a custom package for Telerik a coworker put in our nuget feed, but it says Telerik.WinForms 1.0.0. I'm in Vb.Net, .Net framework 4.5.
I was able to figure this out.
I could load the categories on form load with something like this:
Private Sub frmMyForm_Load(sender As Object, e As EventArgs) Handles Me.Load
'...
LoadAllCategoriesForFilter()
'...
End Sub
Private Sub LoadAllCategoriesForFilter()
Dim categories As List(Of String) = GetCategoriesList()
If categories IsNot Nothing And categories.Count > 0 Then
rgvMyRadGridView.Columns("colCategory").DistinctValues.Clear()
For Each category As String In categories
rgvMyRadGridView.Columns("colCategory").DistinctValues.Add(category)
Next
End If
End Sub
Basically I have been researching for hours on end and cannot find the answer.
Im using Visual Studio 2012, SQL server, using Visual basic.
I want to transfer the value of record from a datagridtable after it is double clicked so that it will link to a page with the value of the previous values.
I have tried selectedrow ect but still cant get it to work, I have also looked into global variables to see if I could transfer it that way but sadly no luck as I need to be able to get the value.
This is what I currently have, it just displays the first record from the table.
Private Sub FlightDataGridView_CellDoubleClick(sender As Object, e As
DataGridViewCellEventArgs) Handles FlightDataGridView.CellDoubleClick
Dim SelectedRowView As Data.DataRowView
Dim SelectedRow As CompleteDataSet.FlightRow
SelectedRowView = CType(FlightBindingSource.Current, System.Data.DataRowView)
SelectedRow = CType(SelectedRowView.Row, CompleteDataSet.FlightRow)
Dim BookForm As New ViewFlights
BookForm.LoadBookForm(SelectedRow.Flight_ID)
BookForm.Show()
End Sub
In the load event for the 'ViewFlights' form I have this:
FlightBookingTableAdapter.FillByFlightID(CompleteDataSet.FlightBooking, Flight_ID:=)
However what goes after 'Flight_ID:='
Thanks for any help
UPDATE: I asked a more general question here: Can I do two levels of linking in Crystal Reports?
I'm using Crystal Reports within VB.NET and Visual Studio 2005.
I have a report with several subreports. I'm setting List(Of MainStuff) as the data sources for the main report. I'm setting List(Of SubreportStuff) as the data source for the subreport. Each SubreportStuff has a key which links back to a particular MainStuff, so the report groups each MainStuff item with its related SubreportStuff items. (Or, at the DB level, SubreportStuff items have a foreign key, which is the primary key in MainStuff.)
Below shows the Load function for the dialog, which contains a CrystalReportsViewer. In the Crystal Reports report editor (within VS 2005) I set the subreport link to pull only the related items into that part of the report.
Imports System.Windows.Forms
Public Class dlgMyReport
Private rpt As New MyReport
Public theMainList As New List(Of MainStuff)
Dim theSubreportList As New List(Of SubreportStuff)
Private Sub dlgMyReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
rpt.SetDataSource(theMainList)
If theSubreportStuff.Count > 0 Then
rpt.Subreports.Item("subReport").SetDataSource(theSubreportList)
End If
Me.StuffViewer.ReportSource = rpt
End Sub
... ' other subs and functions
End Class
This works fine.
Now, though, what I'm needing to do is in essence the same thing, but pull items based on the keys in the subreport items. This would mean having a subreport within a subreport. However, it doesn't appear that I can do this (the option for inserting a subreport is grayed out when I try to insert in something that is already a subreport).
Is there a way that I can accomplish this? (Can I carry the subreport relationship down another level in some way?)
Thanks as always!
Subreports within subreports are not possible within Crystal Reports.