Hello,
Here are two solutions that work. The first creates a new table from one of my dataset tables and then imports the gridview rows that the user has selected, into the table. The returned table is then set as the data source for the report. I'm using CR XI for .NET 2005 release 2.
1st solution
private DataTable GetSelectedRecordsDT()
{
DataSetResponseRecord.ResponseGetDataTable dt = new DataSetResponseRecord.ResponseGetDataTable();
try
{
if (this.dataSetResponseRecord.ResponseGet.Count > 0)
{
foreach (DataGridViewRow row in this.dataGridViewResponseRecords.Rows)
{
DataRowView drv = row.DataBoundItem as DataRowView;
if ((drv.Row != null) && (row.Selected == true))
{
// Add record to the DataTable
dt.ImportRow(drv.Row);
}
}
}
return dt;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return dt;
}
In the CR report designer, I opened the database expert, navigated to Project Data-ADO.NET Datasets and chose my DataSetResponseRecord to bind to the report.
2nd solution:
This solution puts the user selected records from the gridview into an ArrayList collection. The returned ArrayList is then set as the data source for the report. CollectionResponseRecords is a simple class which copies the gridview row fields (responseRec.data1, responseRec.data2, etc.) into it's own private data members.
private ArrayList GetSelectedRecordsCollection()
{
ArrayList aList = new ArrayList();
try
{
if (this.dataSetResponseRecord.ResponseGet.Count > 0)
{
foreach (DataGridViewRow row in this.dataGridViewResponseRecords.Rows)
{
DataRowView drv = row.DataBoundItem as DataRowView;
DataSetResponseRecord.ResponseGetRow responseRec = drv.Row as DataSetResponseRecord.ResponseGetRow;
if ((responseRec != null) && (row.Selected == true))
{
// Add record to the DataSource
aList.Add(new CollectionResponseRecords(responseRec));
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return aList;
}
In the CR report designer, I opened the database expert, navigated to Project Data-.NET Objects and chose my CollectionResponseRecords to bind to the report.
The caller method for either of the two routines above is shown below. FormCR is a windows form which holds the crystalReportViewer control.
public void PrintPreview()
{
try
{
string reportPath = Application.StartupPath + "\\" + "CrystalReport2.rpt";
FormCrystalReport formCR = new FormCrystalReport("Computed Exposure Report");
formCR.reportPath = reportPath;
formCR.dataSource = GetSelectedRecordsDT();
// OR
// formCR.dataSource = GetSelectedRecordsCollection();
formCR.PrintPreview();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Here is a fragment of the CollectionResponseRecords class:
public class CollectionResponseRecords
{
private DateTime readingDate;
private string instrumentID;
private double response1;
private double response2;
...
public CollectionResponseRecords(DataSetResponseRecord.ResponseGetRow rr)
{
readingDate = rr.readingDate;
instrumentID = rr.instrumentID;
response1 = rr.Isresponse1Null() ? double.NaN : rr.response1;
response2 = rr.Isresponse2Null() ? double.NaN : rr.response2;
...
}
}
Hopefully, this code will help other CR newbies. A lot of my brain cells were burned getting this far. Maybe a future version of CR will make it easier to access the DataGridView.
Regards,
Ken