UltraWebGrid uwg1 = null;
        protected void Page_Load(object sender, EventArgs e)
        {            uwg1 = new UltraWebGrid();
            SetStyle();
            uwg1.SortColumn += new SortColumnEventHandler(JeezWebGrid_SortColumn);
            uwg1.PageIndexChanged += new PageIndexChangedEventHandler(UltraWebGrid1_PageIndexChanged);
            if (!this.IsPostBack)
                BindData(uwg1, null);
            this.form1.Controls.Add(uwg1);        }
        private static string _presetContent = null;
        /// <summary>
        /// 控件样式表连接名称.
        /// </summary>
        protected const string CSS_LINK_NAME = "WebGridCss";
        /// <summary>
        /// 控件的样式文件.
        /// </summary>
        protected const string CSS_FILE_PATH = "~/ig_common/20071CLR20/Styles/Office2007Blue/ig_WebGrid.css";
        /// <summary>
        /// 控件的样式配置文件.
        /// </summary>
        protected const string PRESET_FILE_PATH = @"~\ig_common\20071CLR20\Styles\Office2007Blue\WebGrid.xml";        /// <summary>
        /// 获得控件样式配置的内容.
        /// </summary>
        public static StringReader PresetContent
        {
            get
            {
                if (string.IsNullOrEmpty(_presetContent))
                {
                    using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(PRESET_FILE_PATH)))
                    {
                        _presetContent = sr.ReadToEnd();
                    }
                }                return new StringReader(_presetContent);
            }
        }        private void SetStyle()
        {
            if (HttpContext.Current != null)
            {
                System.Web.UI.Page currentPage = HttpContext.Current.CurrentHandler as System.Web.UI.Page;                string strCss = String.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>", currentPage.ResolveClientUrl(CSS_FILE_PATH));
                if (!currentPage.ClientScript.IsStartupScriptRegistered(currentPage.GetType(), CSS_LINK_NAME))
                {
                    currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(), CSS_LINK_NAME, strCss);
                }                uwg1.Style.Add(System.Web.UI.HtmlTextWriterStyle.Position, "absolute");
                uwg1.LoadPreset(PresetContent, true);
                uwg1.DisplayLayout.Pager.Style.Height = Unit.Percentage(1.00);
            }
        }
               private void BindData(UltraWebGrid pUwg, string sort)
        {
            DataSet ds = new DataSet("ds_dry");
            DataTable dt = new DataTable("dt_dry");
            dt.Columns.Add(new DataColumn("num", typeof(int)));            for (int i = 0; i < 20; i++)
            {
                dr = dt.NewRow();
                dr["num"] = i;
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
            if (sort != null && sort != "")
            {
                DataView dv = ds.Tables[0].DefaultView;
                dv.Sort = sort;
                pUwg.DataSource = dv;
                pUwg.DataBind();
            }
            else
            {
                pUwg.DataSource = ds;
                pUwg.DataBind();
            }            foreach(UltraGridColumn ugc in  pUwg.Columns)
            {
                ugc.AllowRowFiltering = false;
            }
        }        protected void JeezWebGrid_SortColumn(object sender, Infragistics.WebUI.UltraWebGrid.SortColumnEventArgs e)
        {
            UltraWebGrid uwg = sender as UltraWebGrid;
            if (Session["sortDirection"]==null)
            {
                Session["sortDirection"] = "asc";
            }            if (Session["sortDirection"].ToString() == "asc")
            {
                 Session["sortDirection"] = "desc";
            }
            else
            {
                Session["sortDirection"] = "asc";
            }
            e.Cancel = true;
            
            //turn off all sort icons in the headers
            for (int i = 0; i < uwg.DisplayLayout.Bands[0].Columns.Count; i++)
            {
                uwg.DisplayLayout.Bands[0].Columns[i].SortIndicator = Infragistics.WebUI.UltraWebGrid.SortIndicator.None;
            }            BindData((UltraWebGrid)sender, this.uwg1.Columns[e.ColumnNo].Key + " " + Session["sortDirection"]);
        }

解决方案 »

  1.   

    通常我们在检索到数据,存入DataTable之后,再绑定到GridView。当用户点击列的名字时,我们让该列自动排序,升序和降序交差出现。这个时候,如果我们还是构造sql语句,重新与数据据服务器联结,再次进行检索,会耗费太多的时间和资源,所以在这里,我们可以用DataView的Sort属性,进行一个快速简单的排序。该属性如下:(如果你想看详解,可以到msdn里搜索一下)DataView.Sort Propertypublic string Sort {get; set;}
    例:dt.DefaultView.Sort="id desc";这样,dt就是按照指定列进行简单排序后的数据集了,直接再绑定到GridView即可。相关方法还有:DataView dv=new DataView(dt);dv.Sort="id desc";或DataView dv=dt.DefaultView;dv.Sort="id desc";注:在这里,dt是我们前面已经实例化的数据集,存放着检索结果,以前使用它绑定到的GridView。当然,你也可以直接使用dv直接绑定到GridView。
      

  2.   

    重写CreateChildControls方法
    protected override void CreateChildControls()
    {
    uwg1 = new UltraWebGrid();
                SetStyle();
                uwg1.SortColumn += new SortColumnEventHandler(JeezWebGrid_SortColumn);
                uwg1.PageIndexChanged += new PageIndexChangedEventHandler(UltraWebGrid1_PageIndexChanged);
                if (!this.IsPostBack)
                    BindData(uwg1, null);
                this.form1.Controls.Add(uwg1);
    }
      

  3.   

    UltraWebGrid 第三方控件,一般都是先重写控件再使用
    排序看看相关属性。
    protected void UltraWebGrid2_SortColumn(object sender, SortColumnEventArgs e)
        {
            string newSortDirection;
            string sqltemp = "";
            if (oldSortDirection == "ASC")
                newSortDirection = "DESC";
            else
                newSortDirection = "ASC";
            oldSortDirection = newSortDirection;
            if (e.ColumnNo != 0)
            {
                if (sqlwhere != "")
                {
                  sqltemp +=" " + UltraWebGrid1.Columns[e.ColumnNo].Key + " " + newSortDirection;
                  moDataView.RowFilter = "1=1" + sqlwhere;//sqlwhere查询条件
                    moDataView.Sort = sqltemp;//按选择字段排序
                    if (moDataView.Count == 0)
                    {
                       UltraWebGrid1.DataSource = moDataView;
                       UltraWebGrid1.DataBind();
                    }
                    else
                    {
                        UltraWebGrid1.DataSource = moDataView;
                        UltraWebGrid1.DataBind();
                    }
                }
                else
                {
                    moDataView.RowFilter = "";
                    UltraWebGrid1.DataSource = moDataView;
                    UltraWebGrid1.DataBind();
                }
             }
        }