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