Use ADO. Net to set the sorting samples of the gridview data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.