How to pass data from Datagridview to Reportviewer

Asked

Viewed 368 times

2

I need to pass data from DataGridView to the ReportViewer to make impression, but as I am still novice in c# I am not able to pass the datatable to the form of ReportViewer, if anyone can explain me a step by step I appreciate.

follows my code from DataGridView

private void bln_gerar_Click(object sender, EventArgs e)
{
    try
    {
        SqlCommand geracomissao = new SqlCommand("usp_RelatorioComissao", conexaoDADOSADV(true));
        geracomissao.Parameters.AddWithValue("@VENDEDOR", this.txt_vendedor.Text);
        geracomissao.Parameters.AddWithValue("@DTINICIAL", this.txt_dtinicial.Text);
        geracomissao.Parameters.AddWithValue("@DTFINAL", this.txt_dtfinal.Text);
        geracomissao.CommandType = CommandType.StoredProcedure;
        geracomissao.ExecuteNonQuery(); 

        SqlDataAdapter dados = new SqlDataAdapter(geracomissao);
        DataTable dtLista = new DataTable();
        dados.Fill(dtLista);

        dgw_comissao.DataSource = dtLista;

        dgw_comissao.Columns["NOTA"].ReadOnly = true;
        dgw_comissao.Columns["CLIENTE"].ReadOnly = true;
        dgw_comissao.Columns["PRODUTO"].ReadOnly = true;
        dgw_comissao.Columns["VALOR PARCELA"].ReadOnly = true;
        dgw_comissao.Columns["PARCELA S/ IMPOSTO"].ReadOnly = true;
        dgw_comissao.Columns["VENCIMENTO"].ReadOnly = true;
        dgw_comissao.Columns["PREÇO VENDA"].ReadOnly = true;
        dgw_comissao.Columns["COMISSÃO VENDEDOR"].Visible = false;
    }
    catch
    {
        MessageBox.Show("Não exstem dados digitados para a consulta, por favor verificar!!!");
        return;
    }
}

follows the code of the form where I am placing the ReportViewer.

public partial class frmImpRelatorioComissao : Form
{
    public frmImpRelatorioComissao()
    {
        InitializeComponent();
    }

    private void frmImpRelatorioComissao_Load(object sender, EventArgs e)
    {

        this.rpw_comissao.RefreshReport();
    }
}

1 answer

1


You need to create and properly initialize the Reportview-specific Datasource: ReportDataSource.

Reportdatasource will represent a (valid) data source for a report.

When calling the report form, pass the Datatable as parameter (which is in form 1).

[I updated the response with a better suggestion. I also added the code for the form call]

Initial form
After filling the Datatable (dtList) with Sqldataadapter (data), instantiate the report form and fill the datatable property.

private void bln_gerar_Click(object sender, EventArgs e)
{
    try
    {
        SqlCommand geracomissao = new SqlCommand("usp_RelatorioComissao", conexaoDADOSADV(true));
        geracomissao.Parameters.AddWithValue("@VENDEDOR", this.txt_vendedor.Text);
        geracomissao.Parameters.AddWithValue("@DTINICIAL", this.txt_dtinicial.Text);
        geracomissao.Parameters.AddWithValue("@DTFINAL", this.txt_dtfinal.Text);
        geracomissao.CommandType = CommandType.StoredProcedure;
        geracomissao.ExecuteNonQuery(); 

        SqlDataAdapter dados = new SqlDataAdapter(geracomissao);
        DataTable dtLista = new DataTable();
        dados.Fill(dtLista);

        dgw_comissao.DataSource = dtLista;

        dgw_comissao.Columns["NOTA"].ReadOnly = true;
        dgw_comissao.Columns["CLIENTE"].ReadOnly = true;
        dgw_comissao.Columns["PRODUTO"].ReadOnly = true;
        dgw_comissao.Columns["VALOR PARCELA"].ReadOnly = true;
        dgw_comissao.Columns["PARCELA S/ IMPOSTO"].ReadOnly = true;
        dgw_comissao.Columns["VENCIMENTO"].ReadOnly = true;
        dgw_comissao.Columns["PREÇO VENDA"].ReadOnly = true;
        dgw_comissao.Columns["COMISSÃO VENDEDOR"].Visible = false;

        //Neste momento, o formulário do report será chamado passando 
        //para a propriedade o DataTable já preenchido.
        frmImpRelatorioComissao frmReport = new frmImpRelatorioComissao();
        frmReport.dataTable = dtLista;
        frmReport.Show();
    }
    catch
    {
        MessageBox.Show("Não exstem dados digitados para a consulta, por favor verificar!!!");
        return;
    }
}

Report form
A property was created to receive the existing datatable in Form1.

public partial class frmImpRelatorioComissao : Form
{
    public DataTable dataTable{ get; set; }

    public frmImpRelatorioComissao()
    {
        InitializeComponent();
    }

    private void frmImpRelatorioComissao_Load(object sender, EventArgs e)
    {
        rprtDTSource = new ReportDataSource(dataTable.TableName, dataTable); 
        this.rpw_comissao.LocalReport.DataSources.Add(rprtDTSource); 
        this.rpw_comissao.RefreshReport(); 
    }
}
  • Ismael sorry my ignorance kk on this question, but as a parameter step my datatable which is in Form1..

  • When do you open the report form? That is, when you give new frmImpRelatorioComissao?

  • 1

    @Juniorguerreiro I updated the answer with a more practical way and also added an example of how to call the form.

  • Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.