求助:datagrid使用ItemCreated后,如何再显示箭头正反排序呢?
private void dg1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{
<!--原本下面默认显示的是无意义的数字,需要根据数字来显示不同的名字。如:e.Item.Cells[0].Text=Znum(e.Item.Cells[0].Text);-->
/////////////////
e.Item.Cells[0].Text= "姓名 ";
e.Item.Cells[1].Text= "性别 ";
e.Item.Cells[2].Text= "年龄 ";
/////////////
/////////////////////////这个里面怎么用正反排序呢,我用了cells[0].Text后,SortCommand事件就无法触发了。
}
}
help!!!!!!
------解决方案--------------------string DescFlag = "▼ ",AscFlag = "▲ ";
static string strSortField = "xxx asc "//默认排序字段
以上是变量的声名
-------------------------
写个绑定方法(绑定Grid数据)在里面这样:
DataTable tbData;
DataView tbView;
tbData = xxx(返回DataTable的方法)
tbView.Sort = strSortField;
dgInfoList.DataSource = tbView;
dgInfoList.DataBind();
-----------------------------------
排序事件中:(点击列表头排序,DataGrid要设置一下排序的字段)
public void ChangeSort(object sender, DataGridSortCommandEventArgs e)
{
string tmpSortField , tmpHeaderText , tmpSortExpression ;
bool blnFlag;
int i;
tmpSortField = Convert.ToString(e.SortExpression.ToLower());
tmpHeaderText = " ";
tmpSortExpression = " ";
blnFlag = false;
if (strSortField.ToLower() == tmpSortField)
{
strSortField = tmpSortField + " DESC ";
blnFlag = true;
}
else
{
strSortField = tmpSortField;
blnFlag = false;
}
for (i = 0;i <=dgInfoList.Columns.Count - 1;i++)
{
tmpSortExpression = Convert.ToString(dgInfoList.Columns[i].SortExpression.ToLower());
tmpHeaderText = Convert.ToString(dgInfoList.Columns[i].HeaderText);
tmpHeaderText = tmpHeaderText.Replace(DescFlag, " ");
tmpHeaderText = tmpHeaderText.Replace(AscFlag, " ");
dgInfoList.Columns[i].HeaderText = tmpHeaderText;
if (tmpSortExpression == strSortField.Replace( " DESC ", " "))
{
if (! blnFlag)
{
dgInfoList.Columns[i].HeaderText = tmpHeaderText + AscFlag;
}
else
{
dgInfoList.Columns[i].HeaderText = tmpHeaderText + DescFlag;
}
}
}
BindDataGrid();//调你绑定数据的方法
}
------解决方案--------------------你的datagrid为什么不事前就把header加上去呢??
------解决方案--------------------#region 修改DataGrid列头样式(ChangeDataGridHeader方法)
/// <summary>
/// 修改DataGrid列头样式
/// </summary>
/// <param name= "dg "> DataGrid对象 </param>
private void ChangeDataGridHeader(DataGrid dg)
{
for (int i = 0;i < dg.Columns.Count - 1;i++)
{
dg.Columns[i].HeaderText = dg.Columns[i].HeaderText.Replace( "▲ ", " ").Replace( "▼ ", " ");
if (ViewState[ "SortField "].ToString() == dg.Columns[i].SortExpression)
{
if (ViewState[ "SortDirection "].ToString() == " DESC ")
dg.Columns[i].HeaderText += "▼ ";
else
dg.Columns[i].HeaderText += "▲ ";
// continue;
}
}
}
#endregion
------解决方案--------------------