Я задал вопрос здесь пару дней назад, но у меня нет ответа.Поэтому я продолжаю поиск и решил свою проблему.Поэтому я хочу дать ответ на это, надеясь, что если кто-то застрянет здесь, то кто-то получит помощь от моего ответа.Ведь мы должны помогать друг другу.Итак, шаг за шагом.
1) Я добавляю Parameter
из Report Data
панели инструментов, щелкнув правой кнопкой мыши, добавив параметр и назвав его paramHeader
.![enter image description here](https://i.stack.imgur.com/efbQh.jpg)
![enter image description here](https://i.stack.imgur.com/iy9PO.jpg)
2) Я добавляю Textbox
в Report .rdlc Design
и Drag and Drop
paramHeader
в Textbox
.
3) Затем я добавляю следующий C#
код в мой PrintReport
метод.
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptAllEmployeeRecord.rdlc");
//Passing Parameter
ReportParameterCollection reportParameters = new ReportParameterCollection();
reportParameters.Add(new ReportParameter("paramHeader", "HIRED EMPLOYEES REPORT"));
this.ReportViewer1.LocalReport.SetParameters(reportParameters);
Здесь ParamHeader
- это имя параметра, которое я добавил на первом шаге, а HIRED EMPLOYEES REPORT
- это строковое значение, которое я хочу показать в TextBox
, который я добавил на втором шаге.
Так что мой общий метод будет
public void PrintHiredEmployeesMethod()
{
//set Processing Mode of Report as Local
ReportViewer1.ProcessingMode = ProcessingMode.Local;
//set path of the Local report
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptAllEmployeeRecord.rdlc");
//Passing Parameter
ReportParameterCollection reportParameters = new ReportParameterCollection();
reportParameters.Add(new ReportParameter("paramHeader", "HIRED EMPLOYEES REPORT"));
this.ReportViewer1.LocalReport.SetParameters(reportParameters);
//creating object of DataSet dsEmployee and filling the DataSet using SQLDataAdapter
DataSetAllEmployee dsemp = new DataSetAllEmployee();
using (SqlConnection con = new SqlConnection(Base.GetConnection))
{
SqlCommand cmd = new SqlCommand(@"SELECT * FROM TableEmployee WHERE Status=@Status", con);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@Status","HIRED");
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
adapt.Fill(dsemp, "dtAllEmployeeRecord");
}
//Providing DataSource for the Report
ReportDataSource rds = new ReportDataSource("DataSetAllEmployee", dsemp.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
//Add ReportDataSource
ReportViewer1.LocalReport.DataSources.Add(rds);
}
и мое Page_Load
событие будет выглядеть так
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (EmployeeID.PrintAllEmployees == "ALL EMPLOYEES")
{
PrintAllEmployeesMethod();
}
else if (EmployeeID.PrintAllEmployees == "HIRED EMPLOYEES")
{
PrintHiredEmployeesMethod();
}
else if (EmployeeID.PrintAllEmployees == "REJECTED EMPLOYEES")
{
PrintRejectedEmployeesMethod();
}
else if (EmployeeID.PrintAllEmployees == "UNVERIFIED EMPLOYEES")
{
PrintUnverifiedEmployeesMethod();
}
else
{
//SOMETHING
}
}
}
ПРИМЕЧАНИЕ , что у меня есть четыре разных метода, но я меняюHeader
Раздел в соответствии с моими потребностями.Например, когда пользователь хочет напечатать HIRED EMPLOYEES REPORT
, затем Header Section
показать HIRED EMPLOYEES REPORT
, если пользователь хочет сгенерировать ALL EMPLOYEES REPORT
, тогда Header Section
должен показать ALL EMPLOYEES REPORT
и т. Д.