Hi kankon,
Check this example. Now please take its reference and correct your code.
HTML
GridView
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnShow" Text="Show in New Window" runat="server" OnClick="ShowRDLC" />
Report
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server">
</rsweb:ReportViewer>
</form>
</body>
</html>
Namespaces
C#
using System.Data;
using Microsoft.Reporting.WebForms;
VB.Net
Imports System.Data
Imports Microsoft.Reporting.WebForms
Code
C#
GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void ShowRDLC(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
foreach (GridViewRow row in gvCustomers.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
string id = row.Cells[1].Text;
string name = (row.FindControl("lblName") as Label).Text.Trim();
string country = (row.FindControl("lblCountry") as Label).Text.Trim();
dt.Rows.Add(id, name, country);
}
}
Session["GridViewRow"] = dt;
string url = "Default.aspx";
string s = "window.open('" + url + "', 'popup_window', 'width=350,height=200,left=150,top=120,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
}
Report
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["GridViewRow"] != null)
{
DataTable dt = Session["GridViewRow"] as DataTable;
if (dt.Rows.Count > 0)
{
Customers dsCustomers = new Customers();
foreach (DataRow dr in dt.Rows)
{
dsCustomers.Tables[0].Rows.Add(dr["Id"].ToString(), dr["Name"].ToString(), dr["Country"].ToString());
}
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
}
}
VB.Net
GridView
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub ShowRDLC(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
For Each row As GridViewRow In gvCustomers.Rows
If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
Dim id As String = row.Cells(1).Text
Dim name As String = CType(row.FindControl("lblName"), Label).Text.Trim()
Dim country As String = CType(row.FindControl("lblCountry"), Label).Text.Trim()
dt.Rows.Add(id, name, country)
End If
Next
Session("GridViewRow") = dt
Dim url As String = "Default.aspx"
Dim s As String = "window.open('" & url & "', 'popup_window', 'width=350,height=200,left=150,top=120,resizable=yes');"
ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
End Sub
Report
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Session("GridViewRow") IsNot Nothing Then
Dim dt As DataTable = TryCast(Session("GridViewRow"), DataTable)
If dt.Rows.Count > 0 Then
Dim dsCustomers As Customers = New Customers()
For Each dr As DataRow In dt.Rows
dsCustomers.Tables(0).Rows.Add(dr("Id").ToString(), dr("Name").ToString(), dr("Country").ToString())
Next
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End If
End If
End If
End Sub
Screenshot