ADO.NET 2.0 - 如何使用 DataView 来排序数据

欲通过 DataView 来排序 DataTable 中的数据,请采用下列方式: 

 

n         当您使用第三个多载版本的 DataView 构建函式来建立 DataView 对象时,即可使用 Sort 参数来决定要根据一或多字段来排序资料。您可以在字段名称之后加上关键词 ASC DESC 以便决定要根据此字段来递增递减排序,预设为递增排序。假如您要根据多个字段栏排序数据,请在各字段之间使用逗号(,)来加以分隔。

 

n         您也可以在建立 DataView 对象之后再设定其 Sort 属性以便决定要如何排序数据。Sort 属性的设定方式与 Sort 参数的设定方式完全相同。

 

n         您可以去设定 DataView ApplyDefaultSort 属性,以便决定是否要根据主索引键来排序数据。如果您将 ApplyDefaultSort 属性设定成 True,表示要根据主索引键来排序数据;如果您将 ApplyDefaultSort 属性设定成 False(此为默认值),表示不要根据主索引键来排序数据。

 

请注意,只有在 Sort 属性为 Null 参考或空字符串,以及当数据表已定义主索引键时,才会套用 ApplyDefaultSort 属性的设定。 

 


图表

程序范例 

 

图表 1 所示的程序示范如何在执行阶段动态设定 DataView Sort 属性,以便让用户能够通过一或两个字段来动态排序数据。兹将程序代码完整列示如下:

 

Option Strict On
Imports
System.Data.SqlClient
Public Class Form1
  Private ds As New DataSet
  Private dv As DataView

  Private Sub Form1_Load(ByVal sender As System.Object, _
   
ByVal e As System.EventArgs) Handles MyBase.Load
  '
利用SqlConnectionStringBuilder 对象来构建连接字符串。
  Dim connectStringBuilder As New SqlConnectionStringBuilder()
  connectStringBuilder.DataSource = "(local)SQLEXPRESS"
  connectStringBuilder.InitialCatalog = "
北风贸易"
  connectStringBuilder.IntegratedSecurity = True

  Try
     Using cn As New SqlConnection(connectStringBuilder.ConnectionString)
       cn.Open()

      
Dim cmdLiming As New SqlCommand( _
         "SELECT
部门, 性别, 员工编号, 身份证字号, 姓名, 地址, 邮政编码, " & _

         "出生日期, 婚姻状况, 雇用日期, 起薪, 目前薪资, " & _
         "
加薪日期 FROM 章立民工作室", cn)

      
Using drLiming As SqlDataReader = cmdLiming.ExecuteReader()

        
  ds.Load(drLiming, LoadOption.OverwriteChanges, _
               New String() {"
章立民工作室"})

         
 '
BindingSource 组件系结至 DataSet 当中的「章立民工作室」资料表。
          
Me.BindingSource1.DataSource = ds.Tables("章立民工作室")

        
  '
DataGridView 控件系结至 BindingSource 组件。
           Me.DataGridView1.DataSource = Me.BindingSource1

      
End Using
     End Using

     For i As Integer = 0 To ds.Tables(0).Columns.Count – 1
         ComboBoxSortColumn1.Items.Add(ds.Tables(0).Columns(i).ColumnName)
     Next

     For i As Integer = 0 To ds.Tables(0).Columns.Count – 1
         ComboBoxSortColumn2.Items.Add(ds.Tables(0).Columns(i).ColumnName)
     Next

     '
建立 DataView 对象
     dv = ds.Tables(0).DefaultView

     Catch ex As Exception
         MessageBox.Show(ex.Message)
     End Try
  End
Sub

 
'
根据用户的选择来设定 DataView 对象的 Sort 属性
  Private Sub btnSort_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSort.Click
    Dim SortExpr As String
    If ComboBoxSortColumn1.SelectedIndex > -1 Then
        SortExpr = ComboBoxSortColumn1.SelectedItem.ToString & _
          IIf(RadioButton1.Checked, " ASC", " DESC").ToString
        If ComboBoxSortColumn2.SelectedIndex > -1 Then
            SortExpr &= _
              ", " & ComboBoxSortColumn2.SelectedItem.ToString & _
              IIf(RadioButton3.Checked, " ASC", " DESC").ToString
       
End If
        dv.Sort = SortExpr
    Else
        MessageBox.Show("
您至少必须选取排序的第一个字段")
   
End If
   End
Sub
End
Class 

 

 

章立民研究室敬上

期待更多精彩,敬请关注:

http://www.china-pub.com/static/jsj_zlm_060824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值