Reportviewer tip-use HTML tags to customize reportviewer field display

Source: Internet
Author: User

The reportviewer10 version released by vs2010 has implemented many functional improvements. One of the improvements is the added support for HTML and style labels,

Although the supported tags are limited, they can at least meet the customer's personalized needs.

Currently, the supported tag types are limited to the basic HTML and style types. The list is as follows:

HTML Tag:

· Hyperlinks: <a href>

· Fonts: <font>

· Header, style and block elements:

· Text format: <B>, <I>, <u>, <S>

· List handling: <ol>, <ul>, <li>

Style (CSS) labels:

· Text-align, text-indent

· Font-family, font-size

· Color

· Padding, padding-bottom, padding-top, padding-right, and padding-left

· Font-weight

For details, refer to: http://msdn.microsoft.com/en-us/library/cc645967.aspx

So how to use HTML and style? Assuming that you have been familiar with how to use reportviewer and know the basic steps for creating a report, let's introduce some technical details step by step.

First, we bind a simple dataset in report designer. This dataset contains information about scenic spots (SCAPE): Scenic Spot name, scenic spot description, scenic spot website, and scenic spot opening date.

Associate a table with this DataSet. We do not display scapesite. Only the other fields are displayed. The following figure shows the output of the report designer:

Then we will add HTML support,

ClickSelect the textbox corresponding to rowdetail in the overview of scenic spots,

ClickSelect the text of Textbox (empty by default ),

Right-click, There will be a pop-up menu, click Create placeholder...

The placeholder attribute dialog box is displayed.

Select the field to be displayed by value and the markup type as HTML. If the field contains the HTML code that can be parsed, the Report field is displayed in HTML format. in this way, the basic settings in report designer are complete.

Finally, we need to add some test data for display. The Code is as follows:

        /// <summary>
/// Fill datatable with html markup data
/// </summary>
/// <param name="table"></param>
private void FillHtmlData(DataTable table)
{
for (int i = 1; i < 100; i++)
{
var dr = table.NewRow();

//Use random color to markup description field
var description = string.Format("<font color=\"{0}\">Description-{1}</font>",
ColorTranslator.ToHtml(Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))), i);

var date = DateTime.Now.AddDays(r.Next() % 2 == 0 ? i : -i);

//Use color and style to markup date field
//If Now > date, render as: text-align=left, color = red
//otherwise render as:text-align=right, color = blue,
var flag = DateTime.Now > date;
var dateString = string.Format("<div text-align=\"{0}\"><font color=\"{1}\">{2}</font></div>",
flag ? "left" : "right",
flag ? "red" : "blue",
date.ToShortDateString());

dr.ItemArray = new object[] { string.Format("Scape-{0}", i), description, "http://www.hzwestlake.com/", dateString };
table.Rows.Add(dr);
}
}

The sample code is derived from a real customer requirement. The customer requires that a date that matches a certain condition be marked in different colors.

We added scapesite when designing the dataset. Naturally, we need to use the same operation to open the placeholder attribute box (skipped)

Select the action tab, select "go to URL", and select "[scapesite]" as the URL. When you click "scapedescription", the system automatically jumps to the corresponding website.

We can consider some more complex requirements. For example, in description, we hope that some keywords can be bold or displayed in different colors, which can be achieved through embedded HTML.

The article is just a simple introduction. A very important feature of programmers is to be able to draw inferences from each other and use various knowledge points and techniques. This is the key.

Download the sample program and two examples:

Common effect:

HTML effect:

Download Demo:

Http://files.cnblogs.com/bloodish/WindowsFormsDemo.zip

Category: C # label: reportviewer, HTML, Style

The reportviewer10 version released by vs2010 has implemented many functional improvements. One of the improvements is the added support for HTML and style labels,

Although the supported tags are limited, they can at least meet the customer's personalized needs.

Currently, the supported tag types are limited to the basic HTML and style types. The list is as follows:

HTML Tag:

· Hyperlinks: <a href>

· Fonts: <font>

· Header, style and block elements:

· Text format: <B>, <I>, <u>, <S>

· List handling: <ol>, <ul>, <li>

Style (CSS) labels:

· Text-align, text-indent

· Font-family, font-size

· Color

· Padding, padding-bottom, padding-top, padding-right, and padding-left

· Font-weight

For details, refer to: http://msdn.microsoft.com/en-us/library/cc645967.aspx

So how to use HTML and style? Assuming that you have been familiar with how to use reportviewer and know the basic steps for creating a report, let's introduce some technical details step by step.

First, we bind a simple dataset in report designer. This dataset contains information about scenic spots (SCAPE): Scenic Spot name, scenic spot description, scenic spot website, and scenic spot opening date.

Associate a table with this DataSet. We do not display scapesite. Only the other fields are displayed. The following figure shows the output of the report designer:

Then we will add HTML support,

ClickSelect the textbox corresponding to rowdetail in the overview of scenic spots,

ClickSelect the text of Textbox (empty by default ),

Right-click, There will be a pop-up menu, click Create placeholder...

The placeholder attribute dialog box is displayed.

Select the field to be displayed by value and the markup type as HTML. If the field contains the HTML code that can be parsed, the Report field is displayed in HTML format. in this way, the basic settings in report designer are complete.

Finally, we need to add some test data for display. The Code is as follows:

        /// <summary>
/// Fill datatable with html markup data
/// </summary>
/// <param name="table"></param>
private void FillHtmlData(DataTable table)
{
for (int i = 1; i < 100; i++)
{
var dr = table.NewRow();

//Use random color to markup description field
var description = string.Format("<font color=\"{0}\">Description-{1}</font>",
ColorTranslator.ToHtml(Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))), i);

var date = DateTime.Now.AddDays(r.Next() % 2 == 0 ? i : -i);

//Use color and style to markup date field
//If Now > date, render as: text-align=left, color = red
//otherwise render as:text-align=right, color = blue,
var flag = DateTime.Now > date;
var dateString = string.Format("<div text-align=\"{0}\"><font color=\"{1}\">{2}</font></div>",
flag ? "left" : "right",
flag ? "red" : "blue",
date.ToShortDateString());

dr.ItemArray = new object[] { string.Format("Scape-{0}", i), description, "http://www.hzwestlake.com/", dateString };
table.Rows.Add(dr);
}
}

The sample code is derived from a real customer requirement. The customer requires that a date that matches a certain condition be marked in different colors.

We added scapesite when designing the dataset. Naturally, we need to use the same operation to open the placeholder attribute box (skipped)

Select the action tab, select "go to URL", and select "[scapesite]" as the URL. When you click "scapedescription", the system automatically jumps to the corresponding website.

We can consider some more complex requirements. For example, in description, we hope that some keywords can be bold or displayed in different colors, which can be achieved through embedded HTML.

The article is just a simple introduction. A very important feature of programmers is to be able to draw inferences from each other and use various knowledge points and techniques. This is the key.

Download the sample program and two examples:

Common effect:

HTML effect:

Download Demo:

Http://files.cnblogs.com/bloodish/WindowsFormsDemo.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.