Labels

Wednesday, July 4, 2007

Sorting Image in GridView

Hi,

 

protected void gridViewPastOrders_OnSort(object sender, GridViewSortEventArgs e)

    {

        m_SortExp = e.SortExpression;

        m_SortDirection = e.SortDirection;

    }

 

protected void gridViewPastOrders_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        try

        {

            if (e.Row.RowType == DataControlRowType.Header)

            {

                if (String.Empty != m_SortExp)

                {

                    int column =GetSortColumnIndex(this.gridViewPastOrders, m_SortExp);

                    if (column != -1)

                        AddSortImage(e.Row, column, m_SortDirection);

                }

            }

        }

        catch (Exception ex)

        {

            HandleException(ex);

        }

    }

 

--------------------------------------------------------------------------------------------

public int GetSortColumnIndex(GridView gridView, String sortExpression)

        {

            if (gridView == null)

                return -1;

 

            foreach (DataControlField field in gridView.Columns)

            {

                if (field.SortExpression == sortExpression)

                {

                    return gridView.Columns.IndexOf(field);

                }

            }

 

            return -1;

        }

 

        public static void AddSortImage(GridViewRow headerRow, int column, SortDirection sortDirection)

        {

            if (-1 == column)

            {

                return;

            }

            Image sortImage = new Image();

            if (SortDirection.Ascending == sortDirection)

            {

                sortImage.ImageUrl = “”;

            }

            else

            {

                sortImage.ImageUrl = “”;

            }

 

            // Add the image to the appropriate header cell.

            headerRow.Cells[column].Controls.Add(sortImage);

        }

--------------------------------------------------------------------------------------------

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

1 comment:

  1. I have done same in my webpage sorting is working fine but it is not showing images can u help me plzzz i hv searched net alot but didnt get any solution. here is my code :-
    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.Header Then
    Dim sortColumnIndex As Integer = GetSortColumnIndex()
    If sortColumnIndex <> -1 Then
    AddSortImage(sortColumnIndex, e.Row)
    End If
    End If

    End Sub
    Private Function GetSortColumnIndex() As Integer
    For Each field As DataControlField In GridView1.Columns
    If field.SortExpression = DirectCast(ViewState("SortExpression"), String) Then
    Return GridView1.Columns.IndexOf(field)
    End If
    Next
    Return -1
    End Function



    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
    Dim sortexp As String = e.SortExpression
    If gridviewsortdirection = SortDirection.Ascending Then
    gridviewsortdirection = SortDirection.Descending
    sortgridview(sortexp, "DESC")
    Else
    gridviewsortdirection = SortDirection.Ascending
    sortgridview(sortexp, "ASC")
    End If
    End Sub
    Private Sub sortgridview(ByVal sortexpression As String, ByVal direction As String)
    Dim dt As New DataTable
    dt = gridbind()
    Dim dv As New DataView(dt)
    dv.Sort = sortexpression + " " + direction
    GridView1.DataSource = dv
    GridView1.DataBind()
    End Sub
    Public Property gridviewsortdirection()
    Get
    If ViewState("sortdirection") = 0 Then
    ViewState("sortdirection") = SortDirection.Ascending
    End If
    Return ViewState("sortdirection")
    End Get
    Set(ByVal value)
    ViewState("sortdirection") = value
    End Set
    End Property
    Private Sub AddSortImage(ByVal columnIndex As Integer, ByVal headerRow As GridViewRow)
    ' Create the sorting image based on the sort direction.
    Dim sortImage As New Image()
    If GridView1.SortDirection = SortDirection.Ascending Then
    sortImage.ImageUrl = "~/images/uparrow.gif"
    sortImage.AlternateText = "Ascending Order"
    Else
    sortImage.ImageUrl = "~/images/downarrow.gif"
    sortImage.AlternateText = "Descending Order"
    End If
    ' Add the image to the appropriate header cell.
    headerRow.Cells(columnIndex).Controls.Add(sortImage)

    End Sub

    ReplyDelete