Hello Team,
In my window form project, am able to sort the data between dates range on the dataGridView but the sorted data doesn't show on the print Preview, but the print Preview print all the data from the database
// this is the prnt button on the DataGridView page;
private void pictureBox11_Click(object sender, EventArgs e)
{
frmFirstTimersReport frm = new frmFirstTimersReport(this);
LoadFirstTimersReportByDate();
frm.LoadFirstTimers(); // this code reference report printview page;
frm.Show();
}
// this code is for sorting data between dates range and is on the DataGridView page;
public void LoadFirstTimersReportByDate()
{
int i = 0;
dataGridView5.Rows.Clear();
cn.Open();
cm = new SqlCommand("Select * from tblFirstTimers where FDate between @StartDate and @EndDate", cn);
cm.Parameters.AddWithValue("@StartDate", FirstTimersDt1.Value.Date);
cm.Parameters.AddWithValue("@EndDate", FirstTimersDt2.Value.Date);
dr = cm.ExecuteReader();
while (dr.Read())
{
i++;
dataGridView5.Rows.Add(i, dr["id"].ToString(), dr["FDate"].ToString(), dr["FName"].ToString(), dr["LName"].ToString(), dr["FGender"].ToString(), dr["FDOB"].ToString(), dr["FLocation"].ToString(), dr["FPhoneNo"].ToString(), dr["FInvitedBy"].ToString(), dr["FPrayerRequest"].ToString());
}
dr.Close();
cn.Close();
}
// this code is on the reportView Page ;
public void LoadFirstTimers()
{
ReportDataSource rptDataSource;
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Reports\rptFirstTimers.rdlc";
this.reportViewer1.LocalReport.DataSources.Clear();
DataSet1 ds = new DataSet1();
SqlDataAdapter da = new SqlDataAdapter();
cn.Open();
da.SelectCommand = new SqlCommand("select * from tblFirstTimers", cn);
da.Fill(ds.Tables["dtFirstTimers"]);
rptDataSource = new ReportDataSource("DataSet1", ds.Tables["dtFirstTimers"]);
reportViewer1.LocalReport.DataSources.Add(rptDataSource);
reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
reportViewer1.ZoomMode = ZoomMode.Percent;
reportViewer1.ZoomPercent = 100;
cn.Close();
}
Ashutosh SinghPosted Apr 20, 2024, 3:26 PM
It seems like you're trying to generate a report based on a date range selected in your
DataGridViewbut the report is always showing all the data from the database instead of the filtered data. Let's adjust your code to pass the filtered data to the report.In your
LoadFirstTimersReportByDate()method, you're correctly retrieving the data based on the date range. However, you're not passing this filtered data to the report. Instead, you're fetching all the data from the database in theLoadFirstTimers()method.Here's how you can modify your code:
LoadFirstTimersReportByDate()to return aDataTablecontaining the filtered data.LoadFirstTimers()method.With these changes, the report should now display only the data within the selected date range from the
DataGridView. Make sure to callLoadFirstTimers()after setting up the report viewer in thefrmFirstTimersReportform.Emmmanuel FIADUFEPosted Apr 22, 2024, 10:02 AM
Hello Ashutosh,
The sorted data is now printing according to date range but it is generating additional error, saying the coonection was not close.
public DataTable LoadFirstTimersReportByDate()
{
DataTable dataTable = new DataTable();
int i = 0;
dataGridView5.Rows.Clear();
cn.Open();
cm = new SqlCommand("Select * FROM tblFirstTimers WHERE FDate BETWEEN @StartDate AND @EndDate", cn);
cm.Parameters.AddWithValue("@StartDate", FirstTimersDt1.Value.Date);
cm.Parameters.AddWithValue("@EndDate", FirstTimersDt2.Value.Date);
SqlDataAdapter da = new SqlDataAdapter(cm);
da.Fill(dataTable);
dr = cm.ExecuteReader();
while (dr.Read())
{
i++;
dataGridView5.Rows.Add(i, dr["id"].ToString(), dr["FDate"].ToString(), dr["FName"].ToString(), dr["LName"].ToString(), dr["FGender"].ToString(), dr["FDOB"].ToString(), dr["FLocation"].ToString(), dr["FPhoneNo"].ToString(), dr["FInvitedBy"].ToString(), dr["FPrayerRequest"].ToString());
}
dr.Close();
cn.Close();
return dataTable;
}
public void LoadFirstTimers()
{
ReportDataSource rptDataSource;
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Reports\rptFirstTimers.rdlc";
this.reportViewer1.LocalReport.DataSources.Clear();
cn.Open();
DataTable filteredData =fmlist.LoadFirstTimersReportByDate();
rptDataSource = new ReportDataSource("DataSet1", filteredData);
reportViewer1.LocalReport.DataSources.Add(rptDataSource);
reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
reportViewer1.ZoomMode = ZoomMode.Percent;
reportViewer1.ZoomPercent = 100;
cn.Close();
}
Emmmanuel FIADUFEPosted Apr 21, 2024, 4:09 PM
Thank you team,
It's working now
Jignesh KumarPosted Apr 21, 2024, 4:52 AM
Hello,
You need to add a filter for your date range on load report preview method,