Change the Column width and Position programmatically in Crystal Reports - vb.net

I want to create a report that has dynamic fields (columns). in the simpler words, there are 15 optional fields for user and the user will select 6 of them to be displayed in report.
so, for solution, I added all the columns in the report. and now I want to hide all non-selected columns and also change the width and position of the visible columns.
how can I do that in vb.net 2010 by coding?

As far as the width is concerned, you could use something like this, which will change the height and width (0 of course will hide it, so change it to a value that suits you).
Dim RptDoc As New ReportDocument
Dim _fldName As FieldObject
_fldName = RptDoc.ReportDefinition.ReportObjects("fieldObjectName")
_fldName.Width = 0
_fldName.Height = 0
About moving an object, you could try something like this:
myReport _myReport = new myReport ();
myReport .Section1.ReportObjects["myline"].Top = 10;
You need to be aware of the Section naming in your report and the correct line reference.

If you want just to visualize the data I would suggest you to use a grid. Crystal report is good for static structures. If you need to export the grid to a pdf or Excel then you can use this tool:
http://r-tag.com/Pages/BlogPost/1
Get a free license here: http://r-tag.com/Pages/CommunityEdition.aspx

Related

VB.net (Dev Express XtraReport)

Developing a VB.net Winform project with Dev ExpresDataSSols. After declaringan open SQL connection, SQL command /SqlGridAdapter, I assigned a DataTable to a Grid Control tool. The GridView from my Grid Control was passed to a Dev Express XtraReport object. The XtraReport successfully showed the DataTable from the GridControl's GridView. - I then attempted to add a Dev Express CalculatedField to my XtraReport, but the CalculatedField is not displayed in the XtraReport. Any ideas on how to correct this code? Thanks!
Dim rep As XtraReport = New XtraReport( )
rep = ReportGenerator.GenerateReport(rep, GridView1)
Dim calcField As New CalculatedField( )
rep.CalculatedFields.Add(calcField)
calcField.Data Source=rep.DataSource
calcField.DataMember=rep.DataMember
calcField.FieldType=FieldType.Int32
calcField.Name="Total"
calcField.Expression="SUM([Field])"
rep.ShowPreview()
Can you confirm that the field that you want to sum is named Field?
Are you sure to have value in the database for that field?
based on the documentation it seem ok.
sorry i can't comment because i'haven't enought reputation..
I see that you've added a calculated field to a report, but have not created a label or table cell that's bound to the calculated field. This is demonstrated in the Creating a Calculated Field (Runtime Sample) topic:
/ Bind a label's Text property to the calculated field
// depending on the report's data binding mode.
if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings)
report.FindControl("xrlabel3", true).DataBindings.Add("Text", null, "Products.myField", "{0:c2}");
else report.FindControl("xrlabel3", true).ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text", "FormatString('{0:c2}', [myField])"));

VB stretch DataGridViewImageColumn images programmatically

What I am doing
I am developing a VisualBasic application where the GUI is separated from the data manipulation (frontend and backend). This particular piece of code keeps track of the Serial Numbers already measured and displays them in the Form as a DGV with the serial number and an image or not.
My code
In a class that the GUI Form instantiates, my data is stored in a DataTable with a BindingSource, with the second column displaying an Image when I tell it to in the program (Nothing in the beginning):
Public SerialNumbersBindingSource As New BindingSource
Public SerialNumbersDataTable As New DataTable
(...)
SerialNumbersBindingSource.DataSource = SerialNumbersDataTable
SerialNumbersDataTable.Columns.Add("Serial", Type.GetType("System.String"))
SerialNumbersDataTable.Columns.Add("Pass", MyImage.GetType)
In my GUI Form code, a DataGridView has its DataSource set to the former DataTable:
DataGridViewSerialNumber.DataSource = MyObject.SerialNumbersDataTable
By just updating the DataTable in my class, the DataGridView updates automatically to reflect the current state of it. I do it like:
SerialNumbersDataTable.Add(CurrentSerial, MyImage)
The results I get
The code works, so I can modify and access the DataTable and the DataGridView autoupdates. But the images are not stretching, so I can only see a small part of them.
What I need
I need the second column named "Pass" in DataGridView to stretch the images.
What have I tried
If I access the column, it is treated like a DGVColumn and not a DGVImageColumn, so the Layout operation fails.
DataGridViewSerialNumber.Columns("Pass").DataGridViewImageCellLayout.Stretch
Microsoft's Docs page tells me to do this, which treats the columns like DGVImageColumn as I need "Pass" to. It fails because the first column is a Text one, not image.
For Each column As DataGridViewImageColumn In DataGridViewSerialNumber.Columns("Pass")
column.ImageLayout = DataGridViewImageCellLayout.Stretch
Next
Also I have tried creating a local DGVImageColumn, modify it and write it onto the original column, but it is read-only.
Dim imageColumn As DataGridViewImageColumn
imageColumn = DataGridViewSerialNumber.Columns("Pass")
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
DataGridViewSerialNumber.Columns("Pass") = imageColumn
I have also tried to do it from the designer. If I click the DGV, arrow to the right and 'Edit Column', I can create the two columns and setup Pass as ImageColumn with Stretched Layout. But when I set up DGVSerialNumbers.Datasource to my DataTable, it adds the DataTable's columns to the DGV's.
Failed DGV with columns added in designer
It's time for you to learn how to cast. If you want to access one column then don't use a loop. Simply access the column you want and then cast it as the type you want to use it as. You also need to actually assign the appropriate value to the appropriate property.
DirectCast(DataGridViewSerialNumber.Columns("Pass"), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
If you want to break that up for clarity:
Dim imageColumn = DirectCast(DataGridViewSerialNumber.Columns("Pass"), DataGridViewImageColumn)
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
Also, be aware that Stretch will not retain the original aspect ratio, so you might want to use Zoom instead.

Dynamically Report Preview

Is there any way to populate the report viewer Dynamically (only labels)?, i what to make a little process where the users can define the label and position on the report, for example:
A)User pick this 2 fields:
1)Beneficiary Name
2) Amount of the payment
B) User pick the position of each label (or better with a drag/drop)
1)Beneficiary: Heigth = 30, width = 200, x = 20, y = 60
2)Amount: Heigth = 30, width = 80, x = 400, y = 60
C) We populate the report viewer so the user can see how his report is going to print.
Once the user save is report and want to print a Check/Payment, by code we only are going to take the label and position and send it to the print directly ("p.print()") not with a ReportViewer control, i only want the report viewer so the user can see how their report will see before printing it (position preview), or there another way to do this?.
I hope you get my idea of what a want to do here, thanks in advance.
(i use an example of 2 fields, the user will select between 30 to 40 different field)
The report definition is simply a XML file (text). As part of your report generate/run process, you could alter the contents of the .rdl file
Start by making a report template that has your fields on the report (with default positions).
Before you run the report, read template, replace (reposition or hide) those fields, save copy under a temp name.
Use your report viewer to run the (modified) copy.
I'm not saying it will be easy though. Your requirements sound tough.
The drag/drop resizing and add/removing controls (that you described) would probably have to be done using some pretty slick JQuery. There are plenty of nice examples of that kind of thing (goog).
Take a look at ReportBuilder. It allows user to create adhock reports on the fly.

Report vewier cannot add parameters

I have managed to set a dataset to my tablix on my report viewer that will hold all items belonging to a bill. The issue I present you with is I cannot seem to add any parameters to my textboxes in my .rdlc report. Is this because I have added a connection to my database? Before I added my connection I could easily add parameters, but not anymore. Take a look at my report:
All the red rectangles is where I would usually have my parameters. Is there a way to programmatically do this?
Dim rpJobNum = New ReportParameter("tbJobNo", JobNum)
rv.rvRdlc.LocalReport.SetParameters(rpJobNum)
rv is my report viewer form
rvRdlc is my report viewer control
tbJobNo is my textbox' job number name
JobNum is a variable which holds the respective value (ex: 10000)
If I execute that,I will receive an error:
Any idea why I cannot seem to add any parameter controls? Perhaps there's another way of doing this?
Figured out why I couldn't add parameters. Pretty noob stuff on my part.
Go to View > Report Data
Or... Ctrl + Alt + D
My apologies, but, I'll answer this if it can help someone!
Dim parmlist As New List(Of ReportParameter)()
parmlist.Add(New ReportParameter("tbJobNo ", JobNum))
print.ReportViewer1.LocalReport.SetParameters(parmlist)
you need to create a list, as i did here

Searching a bound Combo Box for User Input

Alright Gents,
Im trying to search a vb.net combo box for an item. The combo box is already bound to dataset. The display member is set to display just a single column of the selected record. I had it set initially so the objects displayed in the combo were a customized class. In this class I specified all the properties I wanted to keep track of and that seemed to work well. However now that I am using the combo box in its bound state it is much more difficult to manipulate the data.
Mission:
To have the user type a number, if the number is contained in the ComboBox, the combo box should then move to that record so that all the other items bound to that control will update as well.
Research:
I have looked into the System.Windows.Forms.BingingManagerBase class and that seems to have the information I need. I just can't figure out the bridge between that and what I'm trying to do. I want to throw something together so I tried to simply do a SQL search against the dataset for the text in the combobox. Unfortunately that requires late binding and my targeted version of the .Net compact framework does not support that.
Here is an example of the late binding I was attempting. (Im working with VB.net 2005, Compact Framework 3.5 I believe:
For i as integer = 0 to combobox.items.count - 1
dim Dsr as Dataset.Row
dim dv as dataview
Dsr = DirectCase(Dv.row, Dataset.Row)
If Dsr(i).DesiredColumn = DesiredRow.Desiredcolumn then
'Do such and such code
End If
Next
I want to be able to search the dataset for a specific record matching a query. After I find that row matching the query I want to be able to move my combo box to the row found in the SQL query. The main problem seems to be that the Combo works in Datarowviews and my datasets are mostly cast to rows pertaining to the DS.
Anyone have some insight on this it would be much appreciated.
Thanks again!
If you know the item that should be set as selected in the combobox you can just set the ComboBox.SelectedItem Property
If you actually do really need to loop through all the items bound to the combobox then once you reach the correct one you can set the ComboBox.SelectedIndex Property.
For i As Integer = 0 To ComboBox.Items.Count - 1
Dim drv As System.Data.DataRowView = Nothing
Dim desiredColumn As String = String.Empty
drv = DirectCast(ComboBox.Items.Item(i), DataRowView)
desiredColumn = Convert.ToString(drv("Tag"))
Debug.WriteLine(desiredColumn)
Next
This seems to find the column value for every record in the combo box allowing me to find the correct index of the text I am searching for. Like I said though, if I could find a way to search through the list of items in the combobox without having to address each one, I would be grateful.