Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have an issue in sorting data in session.
Below code is wroking fine.
But could not be sorted in grid view order by "PubDate"

I don't know how to do this.

Pls advice me

Maideen

Here is my code

VB
Sub CreatGridDetails()
        Dim dt As New DataTable
        If dt.TableName.Contains("tbl_GridDetails") = True Then
            Exit Sub
        Else
            dt = New DataTable("tbl_GridDetails")

            dt.Columns.Add("pubdate", GetType(String))
            dt.Columns.Add("mainsection", GetType(String))
            dt.Columns.Add("subsection", GetType(String))
            dt.Columns.Add("position", GetType(String))
            dt.Columns.Add("color", GetType(String))
            dt.Columns.Add("sizeh", GetType(Decimal))
            dt.Columns.Add("sizew", GetType(Decimal))
            dt.Columns.Add("volume", GetType(Decimal))
            dt.Columns.Add("rate", GetType(Decimal))
            dt.Columns.Add("clrchg", GetType(Decimal))
            dt.Columns.Add("ldchargeP", GetType(Decimal))
            dt.Columns.Add("ldchargeA", GetType(Decimal))
            dt.Columns.Add("gross", GetType(Decimal))
            dt.Columns.Add("SizeCode", GetType(String))
            dt.Columns.Add("Remarks", GetType(String))
            dt.Columns.Add("Materials", GetType(String))
            dt.Columns.Add("pagenumber", GetType(Decimal))
        End If
        dt.Rows.Clear()
        Session("Grid_Details") = dt
        gv.DataSource = dt
        gv.DataBind()
    End Sub

VB
Sub AddGrid()
    Try

        Dim dt As DataTable = Session("Grid_Details")
        Dim dr As DataRow = dt.NewRow
        dr("pubdate") = Me.txtPubDate.Text
        dr("mainsection") = Me.cboMainSection.SelectedValue
        dr("subsection") = Me.cboSubSection.SelectedValue
        dr("position") = Me.cboPosition.SelectedValue
        dr("color") = Me.cboColor.SelectedValue
        dr("sizeh") = Me.txtSizeH.Text
        dr("sizeW") = Me.txtSizeW.Text
        dr("volume") = Me.txtVolume.Text
        dr("rate") = Me.txtRate.Text
        dr("clrchg") = Me.txtColorCharge.Text
        dr("ldchargeP") = Me.txtLDChgP.Text
        dr("ldchargeA") = Me.txtLDChgR.Text
        dr("gross") = Me.txtGrossTotal.Text
        dr("SizeCode") = Me.cboSizeCode.SelectedValue
        dr("Remarks") = Me.txtRemarksDetails.Text
        dr("Materials") = Me.txtMaterials_D.Text
        dr("pagenumber") = Me.txtPageNo.Text
        dt.Rows.Add(dr)
        Session("Grid_Details") = dt
        Exit Sub

    Catch ex As Exception
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert(ex.Message);", True)
    Finally

    End Try

End Sub


VB
Sub load_Grid_Details()
     If Session("Grid_Details") Is Nothing Then
         gv.DataSource = ""
         gv.DataBind()

     Else
         gv.DataSource = Session("Grid_Details")
         gv.DataBind()

     End If
 End Sub
Posted

1 solution

try like below
VB
Dim dt As DataTable = Session("Grid_Details")
dt.DefaultView.Sort = "PubDate"
gv.DataSource =dt.DefaultView.ToTable()
gv.DataBind()


if you need to sort by Date you need to set column type as Date or DateTime
for example
VB
dt.Columns.Add("pubdate", typeof(DateTime));

when you set values to this column, you need to give DateTime input
VB
dr("pubdate") = DateTime.Now

if you have date in string input then, convert using
VB
dr("pubdate") = DateTime.ParseExact(Me.txtPubDate.Text, "dd-MM-yyyy", Nothing)
 
Share this answer
 
v2
Comments
Maideen Abdul Kader 3-Aug-15 0:16am    
Thanks Mr.Damith

It is working. But there is small issue.
Date format (dd-MM-yyyy)
If user first select date like below order

07-08-2015 , 15-08-2015 , 09-09-2015, 21-09-2015

but grid view shows below order
07-08-2015
09-09-2015
15-08-2015
21-09-2015

is there any advice for this?
Thank you in advance

maideen
Maideen Abdul Kader 3-Aug-15 1:13am    
Thank you Mr.Damith
I have changed format. Now It is working
Maideen
DamithSL 3-Aug-15 1:15am    
You are Welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900