GridView用数据源控件和用DataTable作为数据源的不同

1.使用数据源控件可以自动做排序分页,不需要多余代码,可是由于自动绑定太多操作,反而觉得很不灵活
前台:

<asp:GridViewID="gv_test"DataSourceID="ds_test"AllowPaging="True" PageSize="10"runat="server">

...

</asp:GridView>

<asp:SqlDataSource  runat="server" ID="ds_test" SelectCommand="select * from test" ConnectionString="...">

2.使用DataTable为数据源会发现排序,分页包括修改,取消等操作都需要手动实现

前台:
 <asp:GridView ID="gv_test"  runat="server" 
            OnRowCommand="gv_test_RowCommand"  OnRowUpdating="gv_test_RowUpdating"  OnRowEditing ="gv_test_RowEditing"
            OnRowCancelingEdit ="gv_test_RowCancelingEdit" OnPageIndexChanging ="gv_test_PageIndexChanging"  
            AllowPaging="True"  PageSize="10" AllowSorting="True" >
...
</asp:GridView >
 
 
后台:
 
 
Page_Load: gv_test.SortExpression总是为空,只好将排序字符串储存在新建的属性里    
gv_test.Attributes.Add("Sort""lastUpdateDate desc")
 
 
 
Protected Sub bind()
   gv_test.DataSource = getDataTable_bySQL("select * from test order by "+gv_test.Attributes("Sort"))
   gv_test.DataBind()
End Sub

DataView比DatatTable功能强大些,可以拿到数据后进行排序。当SQL Server的ntext列不支持order by时候,用DataView可以解决问题,如下:
 
 
Protected Sub bind()
   gv_test.DataSource = getDataTable_bySQL("select * from test")
   Dim dv As DataView = New DataView(getTableData_bySQL("select * from test"))
   
 
 
   
   dv.Sort=gv_test.Attributes("Sort")
   gv_test.DataSource=dv
   gv_test.DataBind()
End Sub
    Protected Sub gv_test_Sorting(ByVal sender As ObjectByVal e As GridViewSortEventArgsHandles gv_test.Sorting
        gv_test.EditIndex = -1
        If gv_test.Attributes("Sort") = e.SortExpression + " desc" Then
            gv_test.Attributes("Sort") = e.SortExpression
        Else
            gv_test.Attributes("Sort") = e.SortExpression + " desc"
        End If
        bind()
    End Sub
    Protected Sub gv_test_PageIndexChanging(ByVal sender As ObjectByVal e As GridViewPageEventArgs)
        gv_test.PageIndex = e.NewPageIndex
        gv_test.EditIndex = -1
        bind()
    End Sub
    Protected Sub gv_test_RowCancelingEdit(ByVal sender As System.ObjectByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs)
        gv_test.EditIndex = -1
        bind()
    End Sub
    Protected Sub gv_test_RowUpdating(ByVal sender As System.ObjectByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgsHandles gv_test.RowUpdating
        gv_test.EditIndex = -1
    End Sub
    Public Sub gv_test_RowEditing(ByVal sender As System.ObjectByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
        gv_test.EditIndex = e.NewEditIndex
        bind()
    End Sub
 
 

 

转载于:https://www.cnblogs.com/sui84/p/6777204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值