嗨,大家好,我想通過設置AllowSorting =「True」來排序我的asp網格。我也爲活動添加了代碼,但是我似乎無法使其工作。排序在GridView asp網格
private void PopulateGridView()
{
var a = from c in sample_worker.get()
select new
{
c.CemID,
c.Title,
c.Description
};
grd_sample.DataSource = a;
grd_sample.DataBind();
}
這是填充網格的代碼。我加入這個!下面的IsPostBack ..
排序的代碼..
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void grd_sample_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = grd_sample.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
grd_sample.DataSource = dataView;
grd_sample.DataBind();
}
}
我能做些什麼來解決這個問題..還我能夠把它整理來回? desc - asc - desc。 if(dataTable!= null)也總是爲空。
在此先感謝
謝謝你,我試過你的代碼,它工作。但是我不得不改變一條線。如果(e.SortDirection.ToString()==「升序」)..然後再次,謝謝! – anonymous1110