locked
How to create a report from a collection of DataGridView rows

    Question

  • Hi,

    I have a datagridview with a number of selected rows. What I need to do is be able to create a crystal report that shows the selected rows. Does anyone know where to start with this cause I'm at a loss, the only solution I could come up with is to create a table in a database and add each row and then create a report from the table, this is not idel.

    So if anyone knows where to start please do let me know.

    Thanks
    Jon

    Wednesday, December 06, 2006 11:34 AM

All replies

  • First step is if you know what the schema (columns and data type of the data that will be used) then create a xsd file and create the report off of that.  At runtime you can pass a dataset object to the report.  You can modify your dataset that is used to pass the data to the datagridview to only contain the rows that are selected.

    ReportDocument myReport = new ReportDocument();
    myReport.Load(@"C:\myreport.rpt");

    //create the modified dataset with the rows selected

    myReport.SetDatasource(myDataSet);

    • Proposed as answer by neonprimetime Friday, August 29, 2008 8:46 PM
    Thursday, December 07, 2006 3:03 AM
  • I have a report that pulls data based on what is selected in a combo box.  And believe me, I needed major help with this one and it is still pretty confusing but maybe it will at least give you a starting point.  Here is my stored procedure (the '''' around the end date, are 4 single quotes not two double quotes)

     

    CREATE PROCEDURE [usp_rptSingleUnitReport]
      @EndingDate VarChar(100),
     @Where varchar(1000)
     --
    AS
    SET NOCOUNT ON
    declare @SQL varchar(8000)
    set @sql='
    SELECT     p.ProjectID, p.ProjectDescription, p.FileNumber,  u.UnitDescription, u.Building, u.UnitDescriptionForSort, u.Buyer, ft.TransactionTypeID, ft.TransactionDate, CASE WHEN ft.TransactionTypeId = 1 THEN ft.Amount ELSE 0 END AS CashDeposits,
                          CASE WHEN ft.TransactionTypeID = 2 THEN ft.amount ELSE 0 END AS CashDisburse,  ' + '''' + @EndingDate  + '''' + ' as EndingDate
    FROM         dbo.Project p INNER JOIN
                          dbo.Unit u ON p.ProjectID = u.ProjectID INNER JOIN
                          dbo.FinancialTransaction ft ON fa.FinancialAccountID = ft.FinancialAccountID

    WHERE     ft.UnitID in (' +  @Where + ') AND ft.TransactionDate<= ' + '''' + @EndingDate + ''''

    exec (@SQL)
    GO

    And this is to pass the paramater

    if (objReports.CurrentReportId==28) //Single Unit Report

    {

    System.Data.SqlClient.SqlParameter[] par = new System.Data.SqlClient.SqlParameter[2];

    int size = 0;

    par[size] = new System.Data.SqlClient.SqlParameter("EndingDate", SqlDbType.DateTime);

    par[size].Value = this.dtEndDate.Text;

    size++;

    string selectedRows ="";

    foreach(UltraGridRow dr in dgUnits.Rows)

    {

    if(dr.Selected )

    {

    selectedRows = dr.Cells[UnitDataTable.UnitIDColumnName].Value.ToString()+ "," + selectedRows ;

    }

    }

    char[] MyChar = {','};

    string NewString = selectedRows.TrimEnd(MyChar);

    Console.WriteLine(NewString);

    par[size] = new System.Data.SqlClient.SqlParameter("Where", SqlDbType.VarChar);

    par[size].Value =(NewString);

    size++;

    DataSet ds = objReports.RunReport("usp_rptSingleUnitReport", par);

    ReportViewer rv = new ReportViewer(ds, "rptSingleUnit.rpt");

    rv.Show();

    }


     

    Friday, December 15, 2006 6:47 PM