Crystal reports custom background color - c#

problem definition:
having data with symbolic colors (i.e each teacher is noted with a color)
in DataGridView displaying a simple SELECT query result, I managed to color each row with its teacher's color
using visual studio 2012 and crystal report for VS 13.0.5
public void setColor() {
int index = this.Dgv.Columns["color"].Index;
foreach (DataGridViewRow row in this.Dgv.Rows)
{
System.Drawing.ColorConverter cc = new System.Drawing.ColorConverter();
System.Drawing.Color c = (System.Drawing.Color)cc.ConvertFromString(row.Cells[index].Value.ToString());
row.DefaultCellStyle.BackColor = c;
}
this.Dgv.ClearSelection();
}
I'd like to get a similarly styled report in crystal report but I'm having problems with color manipulation, the far I could get was alternating row color as many articles online help me through.
crystal report color issues I've found while searching were solved by applying the logic in a formula but in my case the colors are within the bound dataset (color values like "WHITE", "#FF0092" etc)
I thought of changing how the color is stored to (rr,gg,bb) to make use of the Color function in crystal report, would the function accept the string instead of 3 parameters?
to wrap my question up:
- is it possible to achieve the same level of color-control in crystal reports as in C#?
- is it possible to string-manipulate the (rr,gg,bb) string to extract the 3 needed parameters?
any pointers are appreciated
please include explanation since I'm a CR newbie
thank you

Not certain I understand the 2nd part. You can definitely achieve total color control.
Try a formula called Color. Define it as:
if {Teacher}='teacher1' then color(1,2,3)
else if ...
for each of the teachers.
A CASE statement will work as well.
Then in the Section Expert for the desired section select color, then 'more' to get to the formula expert. Simply insert the name of the formula you just created - #color

Related

How to generate product receipt in winform application?

I am working on a winform application for the first time and I have a gridview which contains a list of products users have bought.
I have a Print button on click which allows the user to generate a receipt like the one below:
So here I am confused whether I should use "winform default RDLC or Crystal Report" or whether I should generate PDF and then let it print out as receipt, but I am not sure if PDF is a good option for receipt generation or not.
For Crystal Report, I have read that I need to install it and client (who will use this desktop application) had to install Crystal Report and also there is some licensing involve with Crystal Report which I don't want.
Also if I use Crystal Report then I am not sure if it would be possible to generate exactly above receipt (with table formatting) and will it be complicated?
Receipt is bit complicated so is there a better tool or way, or how should I generate receipt I have shown in above image?
Update : Printing paper total size is : 7.50 centimeter and user wants to print all the content in center.
Discount = FinalAmount - MRP;
Customer Name, Mobile No, Bill No, Payment Mode values are entered on the form by user itself.
I am having a Excel file which contains list of products and with each products I have information like ProductId,ProductName,MRP,Tax information like CGST,SGST.
Code to fill gridview from excel file based on Product Id:
using (OleDbConnection cnnxls = new OleDbConnection(strConn))
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
{
oda.Fill(dtProductList);
DataColumnCollection columns = dtProductList.Columns;
if (!columns.Contains("FinalAmount"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "FinalAmount", DataType = typeof(decimal) });
}
if (!columns.Contains("Quantity"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "Quantity", DataType = typeof(int) });
}
DataRow lastRow = dtProductList.Rows[dtProductList.Rows.Count - 1];
lastRow["FinalAmount"] = Convert.ToDecimal(lastRow["MRP"]);
lastRow["Quantity"] = 1;
}
Generate and print the receipts
You can use any report designer tool like RDLC Reports or Crystal Reports to generate a report. RDLC reports are good enough. You can print the RDLC report with or without showing the print dialog. You can also easily export the RDLC report manually or using the code.
If for any reason you don't want to use a reporting tool, as another option you can consider generating HTML report easily using Run-time T4 templates.
Using an RDLC report, how to show multiple fields in a single cell
You can easily use an expression to show multiple values in a single cell. Also as another option, you can use rows in a single row group and show different fields in a single column.
Example 1 - RDLC - Show multiple fields in a single column using expression
The following steps show you how you can display multiple fields in a single column using expression. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
In second column, first data row, right click and choose Expression. (image)
In the expression window, enter the desired expression, for example:
= "UnitPrice: " & Fields!UnitPrice.Value.ToString() & System.Environment.NewLine & "Quantitye: " & Fields!Quantity.Value.ToString()
Example 2 - RDLC - Show multiple fields in a single column using row group
The following steps show you how you can display multiple fields in a single column. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
Right click on row header of the first data row and choose Insert Row → Inside Group - Below (image)
In second column, first data row, right click and choose UnitPrice. (image)
Click on the [UnitPrice], and then press Home and type UnitPrice: (image)
Do the same for Quantity, in the next row in the group.
If you need another row in the group, repeat step 3.
You can setup borders of the cells by selecting them and setting BorderStyle individually for top, left, bottom and right.
Download
You can clone or download an example using expression here:
repository
zip file
A quick and easy way I used before was to generate a html page, and then use the html2pdf library to convert it to a pdf file.
You may also consider this approach since the RDLC reports/Crystal reports may be a overkill for your case.
The RDLC is powerful as well as Crystal reports. You may choose the rdlc which comes close in eliminating licensing costs.
Using RDLC
Data
You need to add datasets Here or data sources to the report which you will manipulate to meet the design and data you want.
Design
On design you just drag and drop controls to your taste. There is a challenge that sometimes what you see on the design may not what be you see on final output so you need to test much.
Printing
You can put a print preview or send directly to a pdf viewer using rdlc. Here is an example.
Conclusion
I think If you have your data generated well on the report, the design and layout won't be much of a problem using both rdlc and crystal reports.
UPDATE
Based on further information provided I have tried to do something that may come close to what you want to achieve.
I have used crystal reports as well as database table to simulate because of time. Otherwise the same can be achieved using rdlc.
The sample table i created
Here is the sample query and results from the database. I have made groups that can be accomodated by the crystal reports. You can do calculated text values using the same to put distinction between the Tax information as well as Transaction Memo.
Here is the final look after tweaking the design. The page layout may also be tweaked with regards to your taste.
Update.
For RDLC I think you need to add datasets for memo data and tax information. Take a look at the below if it comes close. I failed to make a preview there were components I hadn't installed.
for adding 3 columns in one cell
you have two options:
1- Use new line expression
=Fields!MyField1.Value + System.Environment.NewLine + Fields!MyField2.Value
2- Use something like subreport or grouping in rdlc.
the first option seams easier

How to make a horizontal merge in SSRS Dynamically

I am creating a report that generates a timeline calendar. I managed to make it work but now I'm stuck with the new problem. I don't know how to merge the cell column with the same year value dynamically. I have no idea how to do this. I am no expert when creating reports. But I think it's very simple to others. Please help.
You don't merge the cells in order to do that.
You have to create a (parent of month) column group (in tablix or matrix) and set the Year as the group value.

Highlight differences between strings on winform

I am hoping somebody can help me out here and that this questions won't be too vague.
I am trying to determine a way that I can highlight the differences between two text strings within a winform.
I have the original string in one column of a data grid and the string to compare to in a second column. What I would like is to highlight where the comparison string is different within the data grid view itself.
Example:
Thanks for the hep
Thanks for the help!!
where the l, and two ! would be highlighted
I have tried the following (Shout out to Bouam for his help in this Previous Post):
for (int i = 0; i < TextProcessingResults.RowCount; i++)
{
if (TextProcessingResults.Rows[i].Cells[1].Value != null)
{
if ((string)TextProcessingResults.Rows[i].Cells[1].Value != (string)TextProcessingResults.Rows[i].Cells[2].Value)
{
TextProcessingResults.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
}
}
}
But this only highlights which rows are different not at the character level. I'm new to all of this, so is this a crazy endeavor to undertake?
Could anybody help or point me towards a resource that would be useful?
You can create a new DatGridViewColumn based on RichTextBox and then use your new rich text column, to highlight differences in rich text.
This may help you:
RichTextBox Cell in a DataGridView
And here is an ouptput based on this:
You can apply the algorithm you need to compare strings for differences to columns and using this DataGridViewRichTextBoxColumn highlight the differences.
To learn more about creating custom datagridview column types you can take a look at Build a Custom NumericUpDown Cell and Column for the DataGridView Control.

How to Insert Values in Specific Excel Cells From DataGridView

Can you please help me to figure out how I can insert DataGridView values into a pre defined excel template (Into Specific Cells)?
I have a DataGridView on my windows Form which is getting the values from user input. Now I would like to enable users to export the DataGridView values into an excel file (A File like attached excel file).
As far as I know I have to create the headers and add them to the code programmatically but for the DataGridView part, honestly I have no idea how I can do that?
As you can see the Form (Box) is starting from B2 to K2 and end from B21 to K21 Now my question is how i can start importing values from B4 - k4 and so on?
Is there any way I can format the style of the cell (like Background color or font style and size) from C#? I mean generating a form like what is looking in attached Excel programmatically.
Thanks for your time in advance
Not sure if this is what you're looking for but ff you are using Office Interop, you can insert a 2-dimensional array into a range in Excel.
The following snippet might not be correct (no VS nearby and I haven't used excel automation for a long time) but you'll get the picture.
Excel.Range oRange = oSheet.Range("B2",Missing.Value);
oRange.Resize(myArray.GetLength(0),myArray.GetLength(1));
oRange.Value = myArray;

Excel/Visual Studio/C#. How to change the display name of a cell but keep the formula

I have some code that those:
void mActiveSheet_Change(Microsoft.Office.Interop.Excel.Range Target)
{
if (Target.Cells.Formula.ToString().StartsWith("=FR("))
....
}
So whenever someone uses my custom function "=FR" I can handle it.
That's fine but while the formula value for that cell is fine, the display value is "#NAME?"
I presume that's because Excel does not recognise what =FR is.
Is there a way that I can change the display name but not the formula so I can have something other then "#NAME?" displayed?
I tried Target.Cells.Value2 = "Boo"; but that also changes the formula
I think you got this upside down.
The real goal is not to hide #NAME? error; The goal is to create your own function which can be plugged into Excel as any other standard function, like SQRT() or ABS().
Search Google or MSDN for user defined function Excel C# - there are working examples.
You are correct about #NAME?. That is the Excel error code that is displayed when a function is not recognized. You can't suppress this.
One of the 'dirty tricks' that might work is to hide the message. Detect the background color of the cell, and change the font color to match the background. Unless the cell is highlighted (not just selected) this, in effect, hides the #NAME? error message. After you handle your =FR function and return a result, then you can reset the font color to what it was before so the answer can be seen.

Categories