When we use girdview with datasouce to control the sequence, many animations will help us with this combination, such as the sort function of the gridview, you only need to set allowsorting = "true" on girdview to enable auto-sorting. But what if datasouce is not used to provide data through ADO. Net ?? This example provides an exam.
First, set the primary plane. The primary plane only has one dynamic and one empty gridview.
<Asp: button id = "button1" runat = "server" text = ""/> <asp: gridview id = "gridview1" runat = "server" allowsorting = "true"> </ASP: gridview>
Next, some samples of codefile are as follows:
Dim oconns as new objconnstr dim connstr as string = oconns. connstr protected sub button#click (byval sender as object, byval e as system. eventargs) handles button1.click' call function getdata to obtain the information me. gridview1.datasource = getdata ("", "") me. gridview1.databind () end sub protected function getdata (byval sortfield as string, byval sortdict as string) as dataview 'gets the information and returns it to dataview using conn as new sqlconnection (connstr) Conn. open () dim sqltxt as string = "" sqltxt + = "select *" sqltxt + = "from MERs" sqltxt + = "" dim cmd as new sqlcommand (sqltxt, Conn) dim dT as new datatable DT. load (cmmd. executereader) dim DV as dataview DV = DT. defaultview if sortfield <> "" then, sort dim sorttxt as string sorttxt = sortfield if sortdict = "1" then sorttxt + = "DESC" end if DV. sort = sorttxt end if return DV end using end function protected sub gridview1_sorting (byval sender as object, byval e as system. web. UI. webcontrols. gridviewsorteventargs) handles gridview1.sorting dim sortfield as string = E. sortexpression dim sortdir as string = E. sortdirection if viewstate ("sortfield") isnot nothing then if viewstate ("sortfield") = sortfield then sortdir = (CINT (viewstate ("sortdir") + 1) mod 2 ). tostring end if viewstate ("sortfield") = sortfield viewstate ("sortdir") = sortdir me. gridview1.datasource = getdata (sortfield, sortdir) me. databind () end sub
Yan fully stated:
At the time of giving an animation, when a gridview sorting statement is triggered, all the items obtained from E. sortdirection are fixed 0, which leads to a change from small to large each time. In order to handle the first point sorting, the second point reverse sorting, and the third point reverse sorting, xiao meow uses viewstate to record the previous vertex, and then uses the mod 2 method after + 1 to switch between 0 and 1.
Offer you an exam