Reputation:
i got many same text objects in crystal report which i want to change text of them using c#.net this is my code:
TextObject textObj = (TextObject)MyReport.Sections[2].ReportObjects[3];
but i cant change text of textObj
Upvotes: 3
Views: 21888
Reputation: 1
TextObject txt;
txt = (TextObject)rd.ReportDefinition.ReportObjects["Text1"];
txt.Text = "Your Text";
Upvotes: 0
Reputation: 79
Best and easiest way to runtime change Crystal Report textObject value in c#
MyReport is your crystalReport object
(MyReport.ReportDefinition.ReportObjects["YorTextObjectName"] as TextObject).Text = "YorStringValue";
Upvotes: 5
Reputation: 2750
I don't think you reference the location of the text object rather than it's name. So try something like:
CrystalDecisions.CrystalReports.Engine.TextObject txtReportHeader;
txtReportHeader = MyReport.ReportDefinition.ReportObjects["txtHeader"] as TextObject;
txtReportHeader.Text = "My Text";
"txtHeader" would be the name of the text object in your report.
EDIT: You can change the value of a text at runtime. You do not need to delete and create a new label. I adapted the code to work for one of my reports and changed the label "locid" to "My Text". See screenshot. Here is the code:
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
CrystalDecisions.CrystalReports.Engine.TextObject txtReportHeader;
txtReportHeader = CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["text3"] as TextObject;
txtReportHeader.Text = "My Text";
Upvotes: 9
Reputation: 3118
text of text objects cant change my friend. i think that the best way is that you create another text object and then delete your first text object, try this code:
TextObject textObj = (TextObject)MyReport.Sections[2].ReportObjects[3];
string newString = "any thing which you want write as your text";
TextObject newTextObj = MyReport.Sections[2].AddTextObject(newString, textObj.Left, textObj.Top);
newTextObj.Height = textObj.Height;
newTextObj.Width = textObj.Width;
newTextObj.Font = textObj.Font;
MyReport.Sections[2].DeleteObject(textObj);
Upvotes: -1