当前位置:编程学习 > C#/ASP.NET >>

SortExpression 的问题

当gridview 和数据源控件邦定的时候,点击排序 SortExpression 就有值

当我是自定义数据邦定的时候
        String sCon = ConfigurationManager.ConnectionStrings["pubConnectString"].ConnectionString;
        SqlConnection con = new SqlConnection(sCon);
        SqlDataAdapter sda = new SqlDataAdapter("select * from authors", con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        MyGridView1.DataSource = ds;
        MyGridView1.DataBind();
        con.Close();

点击排序的时候SortExpression  ="" 为什么呢。怎么让他出来? --------------------编程问答-------------------- ..up --------------------编程问答-------------------- 顶下 --------------------编程问答-------------------- using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TedaBus.RFPrincipal;

/// <summary>
/// 行政用车申请列表页面
/// 创建:叶明
/// 创建日期:2007-03-09
/// 修改记录:
/// </summary>
public partial class OfficeUseBus_ApplyUseBusList : SimpleCode
{
    DataSet ds = new DataSet("BusUseRecord"); //存储申请记录的数据集

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindUseBusRecordList();
        }
    }
    
    protected void btnShowFilter_Click(object sender, EventArgs e)
    {
        //设置筛选Panel的可见性
        pnlFilter.Visible = !pnlFilter.Visible;
    }

    /// <summary>
    /// 绑定行政用车记录GridView
    /// 涉及数据表:OfficeUseBusRecord
    /// </summary>
    private void BindUseBusRecordList()
    {
        string sql = "SELECT * FROM OfficeUseBusRecord WHERE valid = 1 AND DeptID = " + GetCurrentUserDeptID();
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TedabusMIS"].ToString());
        SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
        sda.Fill(ds);
        DataView dv = ds.Tables[0].DefaultView;
        //对于数据视图的RowFilter选项,我先将其赋值为一个总是为真的条件,然后根据用户的选择逐步增加其限制
        string filterString = "1 = 1";
        //处理开始查询的日期
        if (ViewState["StartDate"] != null)
        {
            if (ViewState["StartDate"].ToString() != string.Empty)
            {
                filterString += " and UserBusDate > #" + ViewState["StartDate"].ToString() + "#";
            }
        }
        //处理最晚查询的日期
        if (ViewState["EndDate"] != null)
        {
            if (ViewState["EndDate"].ToString() != string.Empty)
            {
                filterString += " and UserBusDate < #" + ViewState["EndDate"].ToString() + "#";
            }
        }

        dv.RowFilter = filterString;

        //处理GridView的排序
        if (ViewState["sort"] != null)
        { 
            if(ViewState["sort"].ToString() != string.Empty)
            {
                dv.Sort = ViewState["sort"].ToString();
            }
            else
            {
                dv.Sort = "";
            }
        }

        gvUseBusRecord.DataSource = dv;
        gvUseBusRecord.DataBind();
    }
    
    protected void gvUseBusRecord_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) //只处理数据行
        {
            //将GridView中的日期显示为短日期
            DateTime dt = DateTime.Parse(e.Row.Cells[0].Text);
            e.Row.Cells[0].Text = dt.ToShortDateString();
            //把申请的批准状态映射为中文
            DataRowView row = (DataRowView)e.Row.DataItem;
            e.Row.Cells[0].ToolTip = row["RecordID"].ToString();
            if ((int)row["Status"] == 0)
            {
                e.Row.Cells[5].Text = "未批准";
                e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
            }
            else if ((int)row["Status"] == 1)
            {
                e.Row.Cells[5].Text = "已批准";
                e.Row.Cells[5].ForeColor = System.Drawing.Color.Green;
                //如果申请已经批准了,就不能撤销了
                LinkButton lb = (LinkButton)e.Row.Cells[7].Controls[0];
                lb.Enabled = false;
            }
            //设置查看LinkButton的PostUrl,这样用户点击的时候会转到申请查看的页面
            LinkButton lbView = (LinkButton)e.Row.Cells[8].FindControl("lbView");
            lbView.PostBackUrl = "ViewBusUseApply.aspx?RecordID=" + row["RecordID"].ToString();
            //为撤销按钮添加确认对话框
            LinkButton lbDelete = (LinkButton)e.Row.Cells[7].Controls[0];
            lbDelete.Attributes.Add("OnClick","return confirm('您是否要撤销这个申请?');");
        }
    }
    
    protected void gvUseBusRecord_Sorting(object sender, GridViewSortEventArgs e)
    {
        //把GridView的排序表达式存储在ViewState中,然后重新绑定GridView
        ViewState["sort"] = e.SortExpression;
        BindUseBusRecordList();
    }

    protected void gvUseBusRecord_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //把GridVew得到PageIndex设置为新值,然后重新绑定GridView
        gvUseBusRecord.PageIndex = e.NewPageIndex;
        BindUseBusRecordList();
    }
    
    protected void btnSetFilter_Click(object sender, EventArgs e)
    {
        //用户点击设置筛选条件的按钮时,我把筛选条件存入ViewState,具体的工作,绑定GridView的时候会处理
        ViewState["StartDate"] = txtStart.Text;
        ViewState["EndDate"] = txtEnd.Text;
        BindUseBusRecordList();
    }
    
    protected void gvUseBusRecord_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //点击删除的时候,只是将Valid字段设置为0,并没有物理删除
        //如果其他表中出现过的车辆被"删除",可以避免出现错误
        string sql = "UPDATE OfficeUseBusRecord SET valid = 0 WHERE RecordID = " + gvUseBusRecord.Rows[e.RowIndex].Cells[0].ToolTip ;
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TedabusMIS"].ToString());
        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        if(cmd.ExecuteNonQuery() > 0)
        {
            BindUseBusRecordList();
            RegisterStartupScript("message","<script language='JavaScript'>alert('删除成功');</script>");
        }
        else
        {
            RegisterStartupScript("message","<script language='JavaScript'>alert('删除失败');</script>");
        }
        conn.Close();
        conn.Dispose();
    }

    /// <summary>
    /// 利用当前用户的登录名获取当前用户所在的部门编号
    /// 涉及数据表:EmployeeBaseInfo
    /// </summary>
    /// <returns>当前用户所在的部门编号</returns>
    private string GetCurrentUserDeptID()
    {
        string result = "";
        //获取当前登录的用户的员工编号
        //P.S. 如果无法得到当前用户所在的部门,请在这里Response一下,很可能就是下面的变量没有得到
        string userName = ((SitePrincipal)Context.User).UserCardNum.ToString();

        if (userName != string.Empty)
        {
            string sql = "SELECT DepID FROM EmployeeBaseInfo WHERE EmployeeNo ='" + userName + "'";
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TedabusMIS"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                //因为只有一个字段,我用的是SqlCommand的ExecuteScalar方法
                result = cmd.ExecuteScalar().ToString();
            }
            catch
            {
                result = "";
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
        //Response.Write("dept:" + result + " user " + userName);
        return result;
    }
}
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,