[Solved] GridView Selected IndexChanged event is not working after adding Crystal Report Viewer in ASP.Net

akhter
 
on Dec 29, 2020 06:32 AM
Sample_449822.zip
6123 Views

I am adding crystal report viewer on page, then gridview selectedindexchanged event is not working. 

From<asp:TextBox ID="txtst" runat="server" Width="104px"></asp:TextBox>
&nbsp;
to<asp:TextBox ID="txtend" runat="server" Width="104px"></asp:TextBox>
<asp:Button ID="btn_Print" runat="server" Text="Print" OnClick="btn_Print_Click" Width="72px" />
<cr:crystalreportviewer id="PackView" runat="server" autodatabind="true" />

<asp:GridView ID="GVPacKlist" runat="server" HeaderStyle-BackColor="#66ccff" HeaderStyle-ForeColor="White"
    CssClass="mygrdContent" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AutoGenerateColumns="false" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GVPacKlist_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="PID" ReadOnly="True" HeaderText="Pack_No" />
        <asp:TemplateField HeaderText="Packno">
            <ItemTemplate>
                <a href="BalePack.aspx?PID=<%#Eval("PID") %>"><%# Eval("PID") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Order_Ref_No">
            <ItemTemplate>
                <asp:Label ID="Order_Ref_No" runat="server" Text='<%#Bind("Order_Ref_No")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="QTY">
            <ItemTemplate>
                <asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Label ID="Date" runat="server" Text='<%#Bind("Date","{0:MM/dd/yyyy}")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <%--<asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" />--%>
            </ItemTemplate>
            <EditItemTemplate>
                <%--  <asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" OnClick="OnUpdate" />
                        <asp:LinkButton ID="lnkCancel" Text="Cancel" runat="server" OnClick="OnCancel" />--%>
            </EditItemTemplate>
        </asp:TemplateField>
        <%--  <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />--%>
    </Columns>
    <HeaderStyle BackColor="#66CCFF" ForeColor="White"></HeaderStyle>
    <PagerStyle CssClass="pager"></PagerStyle>
    <RowStyle CssClass="rows"></RowStyle>
    <SelectedRowStyle BackColor="Yellow" />
</asp:GridView>

 

protected void GVPacKlist_SelectedIndexChanged(object sender, EventArgs e)
{
     string PID = GVPacKlist.SelectedRow.Cells[1].Text;
     Response.Redirect("Packrpt.aspx?PID=" + PID);
}

after removing Crystal Report Viewer, then gridview selected index changed event working

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 30, 2020 03:24 AM

Hi akhter,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True"
    OnSelectedIndexChanged="gvEmployees_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="EmployeeID" ReadOnly="True" HeaderText="Pack_No" />
        <asp:TemplateField HeaderText="Packno">
            <ItemTemplate>
                <a href="BalePack.aspx?PID=<%#Eval("EmployeeID") %>">
                    <%# Eval("EmployeeID")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%#Bind("Name")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="City">
            <ItemTemplate>
                <asp:Label ID="City" runat="server" Text='<%#Bind("City")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Label ID="Date" runat="server" Text='<%#Bind("BirthDate","{0:MM/dd/yyyy}")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<hr />
<CR:CrystalReportViewer ID="crPersonInformation" runat="server" AutoDataBind="true"
    EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" />

Namespaces

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Code

C#

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

protected void gvEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
    string PID = gvEmployees.SelectedRow.Cells[1].Text;
    Response.Redirect("Packrpt.aspx?PID=" + PID);
}

private void BindGrid()
{
    gvEmployees.DataSource = GetData("SELECT EmployeeID,FirstName + ' ' + LastName As Name,BirthDate,City FROM Employees");
    gvEmployees.DataBind();
}

protected void Bindreport()
{
    ReportDocument crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"));
    Employees dsPersonInformations = GetEmployees();
    crystalReport.SetDataSource(dsPersonInformations);
    crPersonInformation.ReportSource = crystalReport;
}

private Employees GetEmployees()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees");
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (Employees dsEmployees = new Employees())
            {
                sda.Fill(dsEmployees, "Employee");
                return dsEmployees;
            }
        }
    }
}

private static DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet dsEmployees = new DataSet())
            {
                sda.Fill(dsEmployees, "Employee");
                return dsEmployees;
            }
        }
    }
}

VB.Net

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

    Protected Sub gvEmployees_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim PID As String = gvEmployees.SelectedRow.Cells(1).Text
        Response.Redirect("Packrpt.aspx?PID=" & PID)
    End Sub

    Private Sub BindGrid()
        gvEmployees.DataSource = GetData("SELECT EmployeeID,FirstName + ' ' + LastName As Name,BirthDate,City FROM Employees")
        gvEmployees.DataBind()
    End Sub

    Protected Sub Bindreport()
        Dim crystalReport As ReportDocument = New ReportDocument()
        crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"))
        Dim dsPersonInformations As Employees = GetEmployees()
        crystalReport.SetDataSource(dsPersonInformations)
        crPersonInformation.ReportSource = crystalReport
    End Sub

    Private Function GetEmployees() As Employees
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees")
        Using con As SqlConnection = New SqlConnection(conString)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dsEmployees As Employees = New Employees()
                    sda.Fill(dsEmployees, "Employee")
                    Return dsEmployees
                End Using
            End Using
        End Using
    End Function

    Private Shared Function GetData(ByVal query As String) As DataSet
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand(query)
        Using con As SqlConnection = New SqlConnection(conString)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dsEmployees As DataSet = New DataSet()
                    sda.Fill(dsEmployees, "Employee")
                    Return dsEmployees
                End Using
            End Using
        End Using
    End Function