我有以下asp网格控件:
<asp:GridView ID="gwreportpub" CellPadding="5" AllowPaging="true"
runat="server" PageSize="15" OnPageIndexChanging="GridView1_PageIndexChanging" BackColor="White" Caption=""
AllowSorting="True" OnSorting="grdCause_Sorting"
BorderColor="MidnightBlue"
BorderStyle="Outset" Font-Size="Small" ForeColor="Navy" CssClass="mainBodyPosition" EnableSortingAndPagingCallbacks="True">
<HeaderStyle BackColor="#E7E4DA" />
<AlternatingRowStyle BackColor="#F8F6F0" />
</asp:GridView>
这是后面的c#代码,表格显示但排序不起作用,为什么?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
public partial class ShowReportPub : System.Web.UI.Page
{
private DB_Utility dbu;
protected double size = 1;
private string connectionString;
private OracleConnection connection;
private OracleCommand processNumQuery;
private int indexdropitem;
private int coloreriga = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CONNECTSTRING"] = Request["CONNECTSTRING"];
if (Session["CONNECTSTRING"] == null)
{
Response.Redirect("sessionup.asp?type=ShowReportPub");
}
connectionString = Session["CONNECTSTRING"].ToString();
if (connectionString.IndexOf("DSN=") >= 0)
{
Comuni.Utility util = new Utility();
connectionString = util.ConnStr(connectionString);
Session["CONNECTSTRING"] = connectionString;
}
connection = new OracleConnection(connectionString);
connection.Open();
dbu = new DB_Utility(Session["CONNECTSTRING"].ToString());
}
else
{
connectionString = Session["CONNECTSTRING"].ToString();
if (connectionString.IndexOf("DSN=") >= 0)
{
Comuni.Utility util = new Utility();
connectionString = util.ConnStr(connectionString);
Session["CONNECTSTRING"] = connectionString;
}
connection = new OracleConnection(connectionString);
connection.Open();
dbu = new DB_Utility(Session["CONNECTSTRING"].ToString());
}
if (!IsPostBack)
{
//Response.Write("str :" + Request["param1"] + " - " + Request["param2"]);
processNumQuery = new OracleCommand("select distinct nome_report from rpg_notification",connection);
OracleDataReader reader = processNumQuery.ExecuteReader();
while (reader.Read())
{
dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), reader.GetString(0)));
}
reader.Close();
//if(Request["param1"]!=""){
//dropdownlist1.SelectedValue = Request["param1"];
//}
}
if(Request["txtSchedDate"] == null)
{
//if(Request["param2"]!="")
//txtSchedDate.Text = Request["param2"];
}
else
{
txtSchedDate.Text = Request["txtSchedDate"];
gwreportpub.DataSource = getDatiFromDb();
gwreportpub.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string datarepo = "";
if(Request["txtSchedDate"] != ""){
DateTime schedDate = Convert.ToDateTime(Request["txtSchedDate"]);
datarepo = schedDate.ToString("dd-MM-yyyy");
}
string reportname = dropdownlist1.SelectedItem.Text;
gwreportpub.DataSource = getDatiFromDb();
gwreportpub.DataBind();
gwreportpub.Visible = true;
if (gwreportpub.Rows.Count == 0)
{
tbnotif.Text = "Non vi sono report pubblicati";
tbnotif.Visible = true;
}
else{
tbnotif.Visible = true;
tbnotif.Text = "Tabella dei Report Pubblicati: ";
}
return;
}
public DataSet getDatiFromDb()
{
DataSet dt;
if(Request["txtSchedDate"] != ""){
OracleDataAdapter adapter = new OracleDataAdapter();
OracleCommand orclc = new OracleCommand("select relco, data_report, data_pubblicazione,anno_competenza, mese_competenza, pubblicato, md5, nome_file, path from rpg_notification where pubblicato = :flag and data_report <= TO_DATE(:repdate,'DD/MM/YYYY') and nome_report = :nome order by data_pubblicazione desc ",this.connection);
orclc.Parameters.Add(new OracleParameter(":flag", OracleType.VarChar));
orclc.Parameters.Add(new OracleParameter(":nome", OracleType.VarChar));
orclc.Parameters.Add(new OracleParameter(":repdate", OracleType.VarChar));
orclc.Parameters[":flag"].Value = "Y";
orclc.Parameters[":nome"].Value = dropdownlist1.SelectedItem.Text;
orclc.Parameters[":repdate"].Value = Request["txtSchedDate"].ToString();
adapter.SelectCommand = orclc;
dt = new DataSet("rpg_notification");
adapter.Fill(dt,"rpg_notification");
}
else{
OracleDataAdapter adapter = new OracleDataAdapter();
OracleCommand orclc = new OracleCommand("select relco, data_report, data_pubblicazione,anno_competenza, mese_competenza, pubblicato, md5, nome_file, path from rpg_notification where pubblicato = :flag and nome_report = :nome order by data_pubblicazione desc ",this.connection);
orclc.Parameters.Add(new OracleParameter(":flag", OracleType.VarChar));
orclc.Parameters.Add(new OracleParameter(":nome", OracleType.VarChar));
orclc.Parameters[":flag"].Value = "E";
orclc.Parameters[":nome"].Value = dropdownlist1.SelectedItem.Text;
adapter.SelectCommand = orclc;
dt = new DataSet("rpg_notification");
adapter.Fill(dt,"rpg_notification");
}
return dt;
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
gwreportpub.PageIndex = e.NewPageIndex;
gwreportpub.DataBind();
}
protected void grdCause_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
// You can cache the DataTable for improving performance
gwreportpub.DataSource = getDatiFromDb().Tables[0];
gwreportpub.DataBind();
DataTable dt = gwreportpub.DataSource as DataTable;
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
gwreportpub.DataSource = dv;
gwreportpub.DataBind();
}
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value;
}
}
}