以下是公共函数,适用于以下三种各处
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)));
DataRow dr = null;
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 UltraWebGrid1_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
{
this.uwg1.DisplayLayout.Pager.CurrentPageIndex = e.NewPageIndex;
BindData(this.uwg1, null);
}
以下的分页和排序都可以用
1.使用默认提供的排序方式,以下必须要留意。
UltraWebGrid.DisplayLayout.EnableInternalRowsManagement = true;//如果不设置为true,排序几次之后分页就会有问题。
UltraWebGrid_SortColumn可以没有具体的处理代码。
关键代码如下
protected void Page_Load(object sender, EventArgs e)
{
uwg1 = UltraWebGrid1;
if (!this.IsPostBack)//注意只要做一次。
{
SetStyle();
uwg1.SortColumn += new SortColumnEventHandler(JeezWebGrid_SortColumn);
uwg1.PageIndexChanged += new PageIndexChangedEventHandler(UltraWebGrid1_PageIndexChanged);
BindData(uwg1, null);}
}
2.动态创建的UltraWebGrid使用默认排序,以下必须留意
protected void Page_Load(object sender, EventArgs e)
{
uwg1 = new UltraWebGrid();
//if (!this.IsPostBack)//去掉判断,每次都做。
//{
SetStyle();
uwg1.SortColumn += new SortColumnEventHandler(JeezWebGrid_SortColumn);
uwg1.PageIndexChanged += new PageIndexChangedEventHandler(UltraWebGrid1_PageIndexChanged);
if(!this.IsPostBack) //这一句可以加上判断,也可以不加,应为后面的DataBind()方法会把这个覆盖)
BindData(uwg1, null);
//}
this.form1.Controls.Add(uwg1);
}
3.动态创建的UltraWebGrid使用自定义排序,以下必须留意
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);
}
uwg1.DisplayLayout.EnableInternalRowsManagement = false;
protected void JeezWebGrid_SortColumn(object sender, Infragistics.WebUI.UltraWebGrid.SortColumnEventArgs e)
{
UltraWebGrid uwg = sender as UltraWebGrid;
SortIndicator si = SortIndicator.None;
if (Session["sortDirection"] == null)
{
Session["sortDirection"] = "asc";
}
if (Session["sortDirection"].ToString() == "asc")
{
Session["sortDirection"] = "desc";
si = SortIndicator.Descending;
}
else
{
Session["sortDirection"] = "asc";
si = SortIndicator.Ascending;
}
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;
}
//以下两句至关重要
((UltraWebGrid)sender).DisplayLayout.Bands[0].Columns[e.ColumnNo].SortIndicator =si;
BindData((UltraWebGrid)sender, this.uwg1.Columns[e.ColumnNo].Key + " " + Session["sortDirection"]);
}