解决C1Gridview控件使用DataTable数据源时设置属性不排序的问题。
Step1 新建一个WebForm 添加如下控件也就是C1GridView 并设置对应的属性。<%-- 这是用来展示数据的C1GridView --%>
<wijmo:C1GridView ID="C1GridView1" AllowSorting="true" runat="server">
<Columns>
<wijmo:C1BoundField DataField="Id" HeaderText="数据Id" SortExpression="Id"></wijmo:C1BoundField>
<wijmo:C1BoundField DataField="Name" HeaderText="名称" SortExpression="Name"></wijmo:C1BoundField>
<wijmo:C1BoundField DataField="Age" HeaderText="名称" SortExpression="Age"></wijmo:C1BoundField>
</Columns>
</wijmo:C1GridView>
Step2
页面后台方法写如下:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
private void BindData()
{
DataTable dt = GetDataSource();
string sortExpression = this.C1GridView1.Attributes["SortExpression"];
string sortDirection = this.C1GridView1.Attributes["SortDirection"];
// 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
//给C1GridView1设置数据源
C1GridView1.DataSource = dt;
C1GridView1.DataBind();
}
/// <summary>
/// 这块我就不从数据库取数据了,用datatable来代替
/// </summary>
/// <returns></returns>
private DataTable GetDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Rows.Add(new object[] { "000001", "hekui",12 });
dt.Rows.Add(new object[] { "000002", "zhangyu",13 });
dt.Rows.Add(new object[] { "000003", "zhukundian",20 });
dt.Rows.Add(new object[] { "000004", "liyang",50 });
dt.Rows.Add(new object[] { "000005", "caili",45 });
return dt;
}
protected void C1GridView1_Sorting(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewSortEventArgs e)
{
BindData();
}
排序前
排序后
回复 1楼jian.du的帖子
谢谢你的反馈。
请参考另一个帖子的回复:
http://gcdn.gcpowertools.com.cn/showtopic-18179.html
页:
[1]