Change ASP.Net RDLC Report Font size based on text length using C# and VB.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

is it possible to auto/Custom control size of font in a textbox in rdlc of each item name.

please help thanks.

 

Posted 4 months ago

Hi akhter,

Refer below below steps to perform your action.

First go to report designing and Right Click on field you want to change the font size.

Then click on TextBox Properties will open textbox property window and click on Font tab after that click on Size expression(fx).

The expression window will be open there you have write the expression.

=iif(len(Fields!Address.Value) > 20, "8pt", "12pt")

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:ScriptManager runat="server" />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="400px">
</rsweb:ReportViewer>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;

VB.Net

Imports Microsoft.Reporting.WebForms
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Employees dsEmployees = GetData("SELECT * FROM Employees");
        ReportDataSource datasource = new ReportDataSource("DataSet1", dsEmployees.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}

private Employees GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;

            sda.SelectCommand = cmd;
            using (Employees dsEmployees = new Employees())
            {
                sda.Fill(dsEmployees, "DataTable1");
                return dsEmployees;
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ReportViewer1.ProcessingMode = ProcessingMode.Local
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
        Dim dsEmployees As Employees = GetData("SELECT * FROM Employees")
        Dim datasource As ReportDataSource = New ReportDataSource("DataSet1", dsEmployees.Tables(0))
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(datasource)
    End If
End Sub

Private Function GetData(ByVal query As String) As Employees
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand(query)
    Using con As SqlConnection = New SqlConnection(conString)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsEmployees As Employees = New Employees()
                sda.Fill(dsEmployees, "DataTable1")
                Return dsEmployees
            End Using
        End Using
    End Using
End Function

Screenshot