First, if you are working with Crystal report, I recommend that you see some tutorial, because it is a little complicated to use.
I will give you an example that I use and can be very useful for your learning...
First, I recommend that you use datasets...
I’ve had a lot of problems with objects...
Home screen...
![tela1](https://i.stack.imgur.com/yUApv.png)
private void btnImprimir_Click(object sender, EventArgs e)
{
try
{
List<object> ListParams = new List<object>();
ListParams.Add(dtpPeriodoInicio.Value.ToString("yyyy-MM-dd"));
ListParams.Add(dtpPeriodoFinal.Value.ToString("yyyy-MM-dd"));
ListParams.Add(Convert.ToInt32(String.IsNullOrEmpty(ucTxtBtnFornecedor.TextComponent) ? "0" : ucTxtBtnFornecedor.TextComponent));
ListParams.Add(Convert.ToInt32(String.IsNullOrEmpty(ucTxtBtnProduto.TextComponent) ? "0" : ucTxtBtnProduto.TextComponent));
frmPreviewCompra frmPreviewAnalitica = new frmPreviewCompra("RelatorioConferenciaComprasAnalitica", ListParams);
frmPreviewAnalitica.ShowDialog();
}
catch (Exception ex)
{
UIErrors.ShowErrors("Erro ao tentar realizar a impressão.\nErro: " + ex.Message, MessageBoxIcon.Error);
}
}
Preview Screen
In this screen Voce must have the component Crystaldecisions.Windows.Forms.Crystalreportviewer
public partial class frmPreviewCompra : Form
{
#region Attributes
string _tipoRelatorio;
List<object> _param;
#endregion Attributes
#region Constructors
public frmPreviewCompra()
{
InitializeComponent();
}
/// <summary>
/// Construtor do form de visualização do relatório. É necessário passar a sequência
/// correta de valores na variável Param, pois ela é um array de object na qual no load
/// do form eu carrego esses valores do array para os parametros do relatório.
///
/// Obs: Caso for passado errado o relatório sairá errado as informações.
/// </summary>
/// <param name="TipoRelatorio">Define o tipo de relatório que será carregado</param>
/// <param name="Param">Array com os parametros do relatório</param>
public frmPreviewCompra(string TipoRelatorio, List<object> Param) : this()
{
this._tipoRelatorio = TipoRelatorio;
this._param = Param;
}
#endregion Constructors
#region Form Events
private void frmPreviewCompra_Load(object sender, EventArgs e)
{
string where = String.Empty, fields = String.Empty, join = String.Empty, orderBy = String.Empty;
switch (_tipoRelatorio)
{
case "RelatorioConferenciaComprasAnalitica":
dsCompras dsRelCompras = new dsCompras();
crRelatorioConferenciaComprasAnalitico crComprasAnalitico = new crRelatorioConferenciaComprasAnalitico();
fields = @"empresa.RazaoLst,
concat(nf_entrada.IDParceiroFornec, ' - ', parceiro.Nome) Fornecedor,
nf_entrada.NumeroNota NroNotaEntrada,
nf_entrada.DataEmissao,
nf_entrada.DataEntrada,
nf_entrada_produto.IDProduto,
produto.Descricao DescricaoProduto,
nf_entrada_produto.QtdeProduto,
nf_entrada_produto.TotalLiquido,
(nf_entrada_produto.TotalLiquido / nf_entrada_produto.QtdeProduto) PrecoMedio";
join = @"join parceiro on parceiro.ID = nf_entrada.IDParceiroFornec
join nf_entrada_produto on nf_entrada_produto.IDNFEntrada = nf_entrada.ID
join produto on produto.ID = nf_entrada_produto.IDProduto
join empresa on empresa.ID = nf_entrada.IDEmpresa";
where = @"nf_entrada.DataEntrada between '" + _param[0] + "' and '" + _param[1] + "'" +
" AND (((" + Convert.ToInt32(_param[2]) + " > 0) AND (coalesce(nf_entrada.IDParceiroFornec, 0) = " + Convert.ToInt32(_param[2]) + ")) OR ((" + Convert.ToInt32(_param[2]) + " = 0) AND (coalesce(nf_entrada.IDParceiroFornec, 0) >= 0)))" +
" AND (((" + Convert.ToInt32(_param[3]) + " > 0) AND (coalesce(nf_entrada_produto.IDProduto, 0) = " + Convert.ToInt32(_param[3]) + ")) OR ((" + Convert.ToInt32(_param[3]) + " = 0) AND (coalesce(nf_entrada_produto.IDProduto, 0) >= 0)))";
orderBy = "Fornecedor";
dsRelCompras.Tables["dtRelatorioCompraAnalitica"].Merge(Metodos_NF_Entrada.GetAllNotaFiscalEntrada(fields, where, join, orderBy, "", true));
crComprasAnalitico.SetDataSource(dsRelCompras.Tables["dtRelatorioCompraAnalitica"]);
CrystalDecisions.Shared.ParameterField pComprasDataEntradaIni = new CrystalDecisions.Shared.ParameterField();
CrystalDecisions.Shared.ParameterField pComprasDataEntradaFin = new CrystalDecisions.Shared.ParameterField();
CrystalDecisions.Shared.ParameterField pComprasFornecedor = new CrystalDecisions.Shared.ParameterField();
CrystalDecisions.Shared.ParameterField pComprasProduto = new CrystalDecisions.Shared.ParameterField();
pComprasDataEntradaIni = crComprasAnalitico.ParameterFields["@DataIni"];
pComprasDataEntradaFin = crComprasAnalitico.ParameterFields["@DataFin"];
pComprasFornecedor = crComprasAnalitico.ParameterFields["@Fornecedor"];
pComprasProduto = crComprasAnalitico.ParameterFields["@Produto"];
pComprasDataEntradaIni.CurrentValues.AddValue(Convert.ToDateTime(_param[0]).ToString("dd/MM/yyyy"));
pComprasDataEntradaFin.CurrentValues.AddValue(Convert.ToDateTime(_param[1]).ToString("dd/MM/yyyy"));
pComprasFornecedor.CurrentValues.AddValue(_param[2].ToString());
pComprasProduto.CurrentValues.AddValue(_param[3].ToString());
crViewerCompras.ReportSource = crComprasAnalitico;
break;
case "RelatorioConferenciaComprasSintetica":
break;
}
}
#endregion Form Events
}
Data Sets
I use several projects to separate well the reporters by type... inside it I have 1 dataset and the screen of the report itself.
![tela2](https://i.stack.imgur.com/EVM5p.png)
Report
Now just add the dataset in the report and ready!
![tela4](https://i.stack.imgur.com/FSU7q.png)
If you have problems related to the visual of the report itself... it would be advisable to create another question and let us know by leaving a new comment here with the link.
Any doubt leave a comment...
I also asked a question that I have doubts about, follow the link: http://answall.com/questions/35519/fillr-dataset-com-datatable
– Ariel Cardoso
Ariel... is exactly what I do in the example.
– Danilo Breda