set fontstyle of a column of a crystal report programatically?

陌路散爱 提交于 2019-12-06 13:34:41

Changing Font style,Font Size,and Font at runtime in Crystal Report use following code,this will run properly:

you can use TextObject or FieldObject Depending on your condition.here am using FieldObject.

 FieldObject MyText (FieldObject)Repotrdocumentobject.ReportDefinition.ReportObjects[i];

MyText.ApplyFont(new Font("Arial", 11f,FontStyle.Bold));

here i is number of FieldObject in Crystal Report and 11f is font size

Hopefully this code snippet i just threw together will help:

        ReportDocument rpt = new ReportDocument();
        rpt.Load(@"C:\LT0001_COBDEN.rpt");
        foreach (Area a in rpt.ReportDefinition.Areas)
        {
            string s = a.Name;
        }
        foreach (Section c in rpt.ReportDefinition.Sections)
        {
            string s = c.Name;
        }

        ObjectFormat of = rpt.ReportDefinition.Sections["GroupHeaderSection9"].ReportObjects["Text21"].ObjectFormat;
        TextObject to = (TextObject)rpt.ReportDefinition.Sections["GroupHeaderSection9"].ReportObjects["Text21"];
        to.Color = Color.Red;

        crystalReportViewer1.ReportSource = rpt;
        crystalReportViewer1.Refresh();

you can add CRAXDRT to your references and then use it like this

        CRAXDRT.Report report1 = new CRAXDRT.Report();
        CRAXDRT.Application app1 = new CRAXDRT.Application();


        stdole.IFontDisp myFont;
        report1 = app1.OpenReport("Test.rpt", OpenReportMethod.OpenReportByDefault);

        foreach (CRAXDRT.Section sec in report1.Sections)
        {
            for (int i = 1; i < sec.ReportObjects.Count + 1; i++)
            {
                 object objMain, objChange;
                objMain = report1.Sections[sec.Name].ReportObjects[i];

                try
                {
                    objChange = objMain;
                    CRAXDRT.TextObject to1 = (CRAXDRT.TextObject)objChange;
                    myFont = to1.Font;
                    myFont.Name = "Arial";
                    to1.Font = myFont;

                }
                catch (Exception)
                {
                    try
                    {
                        objChange = objMain;
                        CRAXDRT.FieldObject to1 = (CRAXDRT.FieldObject)objChange;
                        myFont = to1.Font;
                        myFont.Name = "Arial";
                        to1.Font = myFont;
                    }
                    catch (Exception){}
                }
            }

        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!