Reputation: 13161
I have a Crystal Reports report and I want to edit a label programmatically from c#. I can manipulate data source but cannot edit a label.
I'm designing a bill report so I need to display company details, date time and some other information which I can't get from my data source.
Upvotes: 3
Views: 5931
Reputation: 15571
Normally for a bill, where the company name and details of it (like address etc) are shown at the top of the bill. In such a case, what I use is the report header. In that case, you can pass on the text to be shown very easily. The other way of passing on something at runtime will be using report parameter. You can bind the parameter to a field or to a formula. Parameters are also very easy to pass on.
In one occasion, I have used the following code to get the parameters from a report dynamically and bind it to a gridview:
private void GetParameters()
{
//DataTable dt = new DataTable("Params");
string dataTableName = "Params";
//add a tablestyle to the grid so there will be custom columnstyles available
// after the datasource has been set....
DataGridTableStyle ts = new System.Windows.Forms.DataGridTableStyle();
ts.MappingName = dataTableName;
dtgParams.TableStyles.Add(ts);
// DataGridTextBoxColumn
DataGridTextBoxColumn cParamName = new DataGridTextBoxColumn();
cParamName.MappingName = "Parameter";
cParamName.HeaderText = "Parameter";
cParamName.ReadOnly=true;
// Add the column style to the column style collection
ts.GridColumnStyles.Add( cParamName );
// DataGridTextBoxColumn
DataGridTextBoxColumn cType = new DataGridTextBoxColumn();
cType.MappingName = "Data_Type";
cType.HeaderText = "Data Type";
cType.ReadOnly=true;
// Add the column style to the column style collection
ts.GridColumnStyles.Add( cType );
// DataGridTextBoxColumn
DataGridTextBoxColumn cValue = new DataGridTextBoxColumn();
cValue.MappingName = "Value";
cValue.HeaderText = "Value";
cValue.ReadOnly=false;
// Add the column style to the column style collection
ts.GridColumnStyles.Add( cValue );
DataRow dr;
dt.Columns.Add(new DataColumn("Parameter",typeof(string)));
dt.Columns.Add(new DataColumn("Data_Type",typeof(string)));
dt.Columns.Add(new DataColumn("Value",typeof(string)));
// For all the Parameters defined in the report
for(int i=0;i<ReportDoc.DataDefinition.ParameterFields.Count; i++)
{
dr = dt.NewRow();
dr[0] = ReportDoc.DataDefinition.ParameterFields[i].ParameterFieldName;
dr[1] = ReportDoc.DataDefinition.ParameterFields[i].ParameterValueKind;
dr[2] = ReportDoc.DataDefinition.ParameterFields[i].DefaultValues[0];
dt.Rows.Add(dr);
}
DataView source = new DataView(dt);
dtgParams.DataSource = source;
}
And used the following code segment to set parameters:
private void SetParamValue (string paramName, string paramValue)
{
ParameterFieldDefinition PFD = null;
ParameterValues PValues = null;
ParameterDiscreteValue Parm = null;
PValues = new ParameterValues();
PFD = ReportDoc.DataDefinition.ParameterFields[paramName];
Parm = new ParameterDiscreteValue();
Parm.Value = paramValue;
PValues.Add(Parm);
PFD.ApplyCurrentValues(PValues);
}
Upvotes: 2
Reputation: 24132
You will need to make this given Label
a FomulaField
which will then be accessible through the FormulaFieldDefinitions
collection, and you will work with the FormulaFieldDefinition
class object that is of interest for you.
Besides, such company information and the like should always be placed directly on the report itself, that is, the edited RPT file. You want to do this particularly when speaking about the company's logo.
Upvotes: 3
Reputation: 1065
Take a look at the CR Object Model. There are limits to what you can control programatically but this should help.
Upvotes: 0