How to pass DataGridView to Crystal Report?

3

I have a GridView in asp.net and I need to click on a print button to generate a report crystal reports with the data of GridView .

So I have filled my GridView

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from table where cod_user='"+ Session["Cod_user"]+"'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gridview1.Visible = true;
gridview1.DataSource = dt;
gridview1.DataBind();
con.Close();

I use webform.aspx with c #

<CR:CrystalReportViewer ID="RecepF" runat="server" AutoDataBind="true" />
<div class="card-header""><i class="fa fa-table" font-weight: bold;>TABLA</i><button type="button" id="BTN" OnClick="BTN_Click"><span class="fa fa-print"></span>IMPRIMIR</button>  

  <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"  CssClass="table table-bordered table-sm"
  ShowFooter="True">
<emptydatatemplate>
        ¡No se encontraron reportes!  
    </emptydatatemplate>                

    <Columns>
         <%--campos...--%>
        <asp:BoundField DataField="campo1" HeaderText="campo1" ReadOnly="True" SortExpression="campo1" />
     -----muchos campos mas------
         </Columns>
  </asp:GridView> 
    </div>
  </div>

//Evento click del boton
private void button1_Click(object sender, EventArgs e)
    {
        DataSet ds=new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(Int16));
        dt.Columns.Add("NombreUsuario", typeof(string));
        dt.Columns.Add("Nombre", typeof(string));
        dt.Columns.Add("Apellido", typeof(string));
        foreach (**DataGridViewRow** dgr in dataGridView1.Rows)
        {
            dt.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value);
        }
        ds.Tables.Add(dt);
        ds.WriteXmlSchema("Ejemplo.xml");
        CrystalReport1 cr = new CrystalReport1();
        cr.SetDataSource(ds);
        crystalReportViewer1.ReportSource = cr;
        crystalReportViewer1.Refresh();
    }

It shows me an error that says "The name of the type or of the namespace DataGridViewRow was not found, is there a missing directive using?" Thank you Greetings

    
asked by sunflower 15.03.2018 в 17:16
source

1 answer

2

You can do it this way, you only have to modify the line where the report is initialized, assuming that your report will be called like this: ReporteCrystal

SqlDataAdapter sda = 
new SqlDataAdapter("SELECT * from table where cod_user='"+ Session["Cod_user"]+"'", con);

DataTable dt = new DataTable();
sda.Fill(dt);

RegisCrystalReport cos = new RegisCrystalReport();
cos.SetDataSource(dt);

crystalReportViewer1.ReportSource = cos;
crystalReportViewer1.Refresh();

In the aspx to create a button that you can call a method would be like this:

<asp:Button runat="server" Text="IMPRIMIR" OnClick="ImprimirReporte_Click"/>

This is an example to fill it with a dataSet that is another way to fill out the report without making the query again, just that you have to add the columns of the report

  

Note: dataGridView1.DataSource = dt;

 private void button1_Click(object sender, EventArgs e)
    {
        DataSet ds=new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(Int16));
        dt.Columns.Add("NombreUsuario", typeof(string));
        dt.Columns.Add("Nombre", typeof(string));
        dt.Columns.Add("Apellido", typeof(string));
        foreach (DataGridViewRow dgr in dataGridView1.Rows)
        {
            dt.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value);
        }
        ds.Tables.Add(dt);
        ds.WriteXmlSchema("Ejemplo.xml");
        CrystalReport1 cr = new CrystalReport1();
        cr.SetDataSource(ds);
        crystalReportViewer1.ReportSource = cr;
        crystalReportViewer1.Refresh();
    }
    
answered by 15.03.2018 в 18:03