1

我正在使用链接按钮从网格视图中显示所选报告 ID 的 pdf 报告。在页面加载时,我将数据源与 Gridview 绑定。通过单击链接按钮,它会从第 0 列获取正确的报告 ID 并显示报告。对网格进行排序后,网格视图会显示排序后的数据。现在,如果我单击链接按钮,它不会从第 0 列中选择更改的值。而是在排序之前选择值。

说:第 0 列在排序前具有值 1、2、3、4、5。如果我在排序之前单击第 3 行的链接按钮,则从第 0 列中选取值 3。排序后为 3、4、5、2、1。现在,如果我单击第 3 行的链接按钮,它仍然选择值 3 而不是 5。你能帮帮我吗?

下面是我的代码:aspx:

CS:

protected void Page_Load(object sender, EventArgs e)
{
     bindGrid();
}
protected void GvStockTakingReport_Sort(object sender, GridViewSortEventArgs e)
    {

            GvStockTakingReport.DataSource = null;
            GvStockTakingReport.DataBind();
            GvStockTakingReport.Dispose();
            DataSet ds = StockTakingList.BindStocktakingReportGrid();
            DataTable dtSortTable = ds.Tables[0];
            if (dtSortTable != null)
            {
                DataView dv = new DataView(ds.Tables[0]);
                dv.Sort = e.SortExpression + " " + getSortDirectionString();
                //ViewState["sortExpression"] = e.SortExpression;
                //Session["sortExpression"] = e.SortExpression;
                GvStockTakingReport.DataSource = dv;
                GvStockTakingReport.DataBind();
            }                      
       }

   protected void PrintReport(object sender, CommandEventArgs e)
    {

            LinkButton lkButton = (LinkButton)sender;
            GridViewRow item = (GridViewRow)lkButton.NamingContainer;

            string Id = (item.FindControl("lblstk_id") as Label).Text;

            //Getting value for Id    
            string Id1 = e.CommandArgument.ToString();


            int rowIndex = Convert.ToInt32((string)e.CommandArgument);


            //string Id1 = Convert.ToString(GvStockTakingReport.Rows[rowIndex].Cells[0].Text);

            string Id2 = ((Label)(GvStockTakingReport.Rows[rowIndex].FindControl("lblstk_id"))).Text;

            //Getting value for Id

            string virtualPath = string.Format("~/{0}/{1}{2}", Portal.Business, Portal.Core.Profile.ReportsDirectory, "General/RPT_03002_StockTakinReport.rpt");
            string physicalPath = Server.MapPath(virtualPath);

            using (ReportDocument report = new ReportDocument())
            {
                report.Load(physicalPath);
                Core.Security.CrystalReportLogOn(report, (SqlConnectionStringBuilder)Portal.Core.Profile.ConnectionStrings["MTServer"]);
                report.SetParameterValue("@STK_Id", Id);
                report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, true, "Stock Taking report");
            }          
    }
4

1 回答 1

0

问题是每当我单击链接按钮时,页面加载都会调用 bindgrid 方法,该方法在 printreport 方法调用之前将 gridview 重置为原始排序。我已经在 bindgrid 方法中包含了排序逻辑,并在下面解决了这个问题。

   public void bindGrid()
    {
        try
        {
            DataSet ds = StockTakingList.BindStocktakingReportGrid();
            GvStockTakingReport.DataSource = ds;
            GvStockTakingReport.DataBind();

            //To make sure the grid remains with sorting order while pageload method invokes otherwise it will reset to original order
            DataView dv = new DataView(ds.Tables[0]);
            if (dv != null)
            {
                if (ViewState["sortExpression"] == null)
                {
                    ViewState["sortExpression"] = "STK_Id"; //First time it will sort by STK_ID i.e on first time page loading
                }

                if (ViewState["sortDirection"] == null)
                {
                    ViewState["sortDirection"] = "ASC";//First time it will sort ascending i.e on first time page loading
                }

                dv.Sort = ViewState["sortExpression"].ToString() + " " + ViewState["sortDirection"].ToString();
                GvStockTakingReport.DataSource = dv;
                GvStockTakingReport.DataBind();
            }
        }
        catch (Exception ex)
        {
            error.Visible = true;
            error.Text = ex.Message.ToString();
        }
    }
于 2013-02-14T07:27:06.297 回答