Filter RDLC report based on ASP.Net GridView selected row using C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

Hello Sir,

I'm trying to display the data based on date range given from ajax calender extender.

I am able to display in gridview but not getting how to pass selected items to rdlc reports

Please help me

Thanks

Posted one year ago

Hi democloud,

Please refer below sample.

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div class=" input-group date form_date">
    from:
    <asp:TextBox ID="txtFrom" class="form-control years hasDatepicker" placeholder="Enter Date"
        runat="server"></asp:TextBox>
    To :
    <asp:TextBox ID="txt_todate" class="form-control years hasDatepicker" placeholder="Enter Date"
        runat="server"></asp:TextBox>
    <span class="input-group-addon"><a href="#"><i id="Image2" class="fa fa-calendar"></i>
    </a>
        <cc1:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txt_todate"
            CssClass="" Format="MM/dd/yyyy" Enabled="True" PopupButtonID="txt_todate" />
    </span><span class="input-group-addon"><a href="#"><i id="I1" class="fa fa-calendar">
    </i></a>
        <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtFrom"
            CssClass="" Format="MM/dd/yyyy" Enabled="True" PopupButtonID="txtFrom" />
    </span>
    <asp:Button ID="Button1" Text="Display" runat="server" OnClick="Display" />
    <asp:GridView runat="server" AutoGenerateColumns="false" ID="gvEmployees">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Country" HeaderText="Country" />
            <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" DataFormatString="{0:dd/MM/yyyy}" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnShow" Text="Show in Report" runat="server" OnClick="ShowRDLC" />
    <hr />
    <rsweb:ReportViewer ID="ReportViewer1" runat="server">
    </rsweb:ReportViewer>
</div>
</div>

Namespaces

C#

using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Microsoft.Reporting.WebForms;

VB.Net

Imports Microsoft.Reporting.WebForms
Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindGrid();
    }
}

protected void Display(object sender, EventArgs e)
{
    BindGrid();
}

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT FirstName +' '+ LastName Name, Country, BirthDate FROM Employees";
    using (SqlConnection con = new SqlConnection(constr))
    {
        SqlCommand cmd = new SqlCommand(query, con);
        if (!string.IsNullOrEmpty(txtFrom.Text) || !string.IsNullOrEmpty(txt_todate.Text))
        {
            query += " WHERE BirthDate BETWEEN @fromDate AND @ToDate";
            cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@fromDate", txtFrom.Text);
            cmd.Parameters.AddWithValue("@ToDate", txt_todate.Text);
        }
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            DataTable dt = new DataTable();
            da.Fill(dt);
            this.gvEmployees.DataSource = dt;
            this.gvEmployees.DataBind();
        }
    }
}

protected void ShowRDLC(object sender, EventArgs e)
{
    DataSet1 ds = new DataSet1();
    foreach (GridViewRow row in gvEmployees.Rows)
    {
        if ((row.FindControl("chkSelect") as CheckBox).Checked)
        {
            string name = row.Cells[1].Text;
            string country = row.Cells[2].Text;
            string birthDate = row.Cells[3].Text;
            ds.Tables[0].Rows.Add(row.Cells[1].Text, row.Cells[2].Text, row.Cells[3].Text);
        }
    }
    ReportViewer1.ProcessingMode = ProcessingMode.Local;
    ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
    ReportDataSource datasource = new ReportDataSource("DataSet1", ds.Tables[0]);
    ReportViewer1.LocalReport.DataSources.Clear();
    ReportViewer1.LocalReport.DataSources.Add(datasource);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        BindGrid()
    End If
End Sub

Protected Sub Display(ByVal sender As Object, ByVal e As EventArgs)
    BindGrid()
End Sub

Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT FirstName +' '+ LastName Name, Country, BirthDate FROM Employees"
    Using con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand(query, con)
        If Not String.IsNullOrEmpty(txtFrom.Text) OrElse Not String.IsNullOrEmpty(txt_todate.Text) Then
            query += " WHERE BirthDate BETWEEN @fromDate AND @ToDate"
            cmd = New SqlCommand(query, con)
            cmd.Parameters.AddWithValue("@fromDate", txtFrom.Text)
            cmd.Parameters.AddWithValue("@ToDate", txt_todate.Text)
        End If

        Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim dt As DataTable = New DataTable()
            da.Fill(dt)
            Me.gvEmployees.DataSource = dt
            Me.gvEmployees.DataBind()
        End Using
    End Using
End Sub

Protected Sub ShowRDLC(ByVal sender As Object, ByVal e As EventArgs)
    Dim ds As DataSet1 = New DataSet1()
    For Each row As GridViewRow In gvEmployees.Rows
        If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
            Dim name As String = row.Cells(1).Text
            Dim country As String = row.Cells(2).Text
            Dim birthDate As String = row.Cells(3).Text
            ds.Tables(0).Rows.Add(row.Cells(1).Text, row.Cells(2).Text, row.Cells(3).Text)
        End If
    Next

    ReportViewer1.ProcessingMode = ProcessingMode.Local
    ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
    Dim datasource As ReportDataSource = New ReportDataSource("DataSet1", ds.Tables(0))
    ReportViewer1.LocalReport.DataSources.Clear()
    ReportViewer1.LocalReport.DataSources.Add(datasource)
End Sub

Screenshot