How to pass data from the datagridview to the reportviwer

2

Good afternoon. I need to pass data from the datagridview to the reportviwer to print, but as I am new to C # I am not able to pass the datatable to the reportview form, if someone can explain me a step by step I would appreciate it.

Follow my datagridview code

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;
    }
}

The form code is where I'm putting reportview.

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

    private void frmImpRelatorioComissao_Load(object sender, EventArgs e)
    {

        this.rpw_comissao.RefreshReport();
    }
}
    
asked by anonymous 25.10.2017 / 17:37

1 answer

1

You need to correctly create and initialize the DataSource specific to ReportView: ReportDataSource .

The ReportDataSource will represent a (valid) data source for a report.

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

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

Initial Form
After filling the DataTable (dtList) with the SqlDataAdapter (data), it instantiates the report form and populates 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(); 
    }
}
    
25.10.2017 / 18:57