Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Identify a sorted column
Message
Information générale
Forum:
ASP.NET
Catégorie:
WebForms
Divers
Thread ID:
00867190
Message ID:
00867813
Vues:
18
Troy,

I realized that this DataGrid was a custom one I created for a client. This technique would not work for you. Sorry about the confusion. For you, I would recommend creating template columns and then modify the contents of the controls inside the templates. You could create two images, one for the down arrow and one for the up arrow. Then turn visible on/off for sorting. I don't have a working example but you should be able to do this without too much work.

>Thank you Cathi, for a very useful code snippet. I am trying to convert the VB code sample you gave me to C# in the context of the page I am attempting this technique, but I am having trouble getting a reference to the header object to add the label control. You use a 'ReportHeader' object which I cannot seem to be able to create. In the code below, which is certainly a sloppy re-write of your sample, the Controls.add seems to think that e.Item is a column and will not allow me to add the object.
>
>
>private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
>{
>	if (e.Item.ItemType==ListItemType.Header)
>	{
>		for (int i=0; i<e.Item.Controls.Count; i++)
>		{
>			try
>			{
>				string LabelText="";
>				DataGrid Grid = (DataGrid)sender;
>				System.Web.UI.WebControls.DataGridColumn Col = Grid.Columns[i];
>				string SortAttribute = Col.SortExpression.Trim().ToLower();
>				if(this.SortOrder.Trim().ToLower().StartsWith(SortAttribute))
>				{
>					if (this.SortOrder.ToLower().IndexOf(" desc")>0)
>						LabelText=" 5";
>					else
>						LabelText=" 6";
>					System.Web.UI.WebControls.Label ArrowLabel = new Label();
>					ArrowLabel.Font.Name="webdings";
>					ArrowLabel.Font.Size=FontUnit.XSmall;
>					ArrowLabel.Text=LabelText;
>					e.Item.Controls.Add(ArrowLabel);
>				}
>				string CurSortOrder = this.SortOrder;
>			}
>			catch (Exception ex)
>			{
>				string ErrorMsg = ex.Message;
>			}
>		}
>	}
>}
>
>
>
>>Hi Troy,
>>
>>Here is some sample code that will add the up or down arrow to the datagrid for the sort column. The best way to get ultimate control over this is to use the ItemCreated event of the DataGrid. When the item is created, a check is made to see if it is actually the header (since this event fires for header, footer, pager, etc.). If the current item/column is the one the grid is sorted by, then add a label control with the up/down arrow in the webding font, giving the directional arrow indicating the sort-direction. Instead of a label control, you could use an image control.
>>
>>
>>Private Sub ReportGrid_ItemCreated(ByVal sender As Object, ByVal e As
>>System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyBase.ItemCreated
>>
>>  If e.Item.ItemType = ListItemType.Header Then
>>    Dim i As Integer = 0
>>    For i = 0 To e.Item.Controls.Count - 1
>>      If Attributes("SortExpression") = Columns(i).SortExpression Then
>>        Dim ArrowLabel As New Label()
>>        ArrowLabel.Font.Name = "webdings"
>>        ArrowLabel.Font.Size = FontUnit.XSmall
>>        If Attributes("SortAsc") = "True" Then
>>          ArrowLabel.Text = " 6"
>>        Else
>>          ArrowLabel.Text = " 5"
>>        End If
>>        ReportHeader.Controls.Add(ArrowLabel)
>>      End If
>>    Next
>>  End If
>>End Sub
>>
>>
>>
>>>I can't imaging this being difficult, but I have not been having any luck on a solution of making the column that represents the current sort order of an ASP.NET grid stand out. I want the column header to somehow stand out if that column represents column header that was last clicked and therefore drives the sort order.
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform