发布网友 发布时间:2022-05-12 13:00
共1个回答
热心网友 时间:2023-10-09 09:55
DataTable 的原始输出格式可以透过DefaultView 属性来取得, DefaultView 属性本身就是DataView 对象 ;我们可以设定DefaultView 的属性来指定DataTable 的显示格式。如果预设的一个DefaultView 这个DataView 对象无法满足你的需求,我们还可以产生多个DataView 对象来制定多个数据显示外观。下表列出了DataView 对象的常用的属性及方法: 排序数据 要排序数据,可以使用DataView 对象的Sort 属性。Sort 属性是以字段做为排序的依据,其设定语法如下所示: DataView.Sort="字段一ASC|DESC [,...字段N ASC|DESC]" 下列范例指定了Members 这个DataTable 的DefaultView 的Sort 属性以UserId 做升幂排序: <%@Import Namespace=System.Data.ADO%> <%@Import Namespace=System.Data%> <Script Language="VB" Runat="Server"> Sub Page_Load(Sender As Object, e As EventArgs) Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb" Dim strComStr As String = "Select * From Members" Dim dscA As ADODataSetCommand = New ADODataSetCommand(strComStr, strConStr) Dim dsDataSet As DataSet = New DataSet() dscA.FillDataSet(dsDataSet, "Members") Dim dtTable As DataTable = dsDataSet.Tables("Members") 为了方 便使用而宣告的别名 Dim shtR As Short Response.Write("DataTable 中的原始资料:<br>") For shtR=0 To dtTable.Rows.Count-1 直接抓出DataTable 中的数 据 Response.Write("顺序" & (shtR+1).ToString & " " & _ dtTable.Rows(shtR)("UserId") & " ") Next dtTable.DefaultView.Sort="UserId Asc" 设定DefaultView 的排序 条件 Response.Write("<p>DataTable.DefaultView 的资料:<br>") For shtR=0 To dtTable.DefaultView.Count-1 显示DataView 整 理的资料 Response.Write("顺序" & (shtR+1).ToString & " " & _ dtTable.DefaultView(shtR)("UserId") & " ") Next End Sub </SCRIPT> 我们可以利用DataView 的Rows 集合取回被DataView 对象整理过的数据,所以上述范例的执行结果如下所示: 筛选记录 要筛选记录,可以使用DataView 对象的RowFilter 属性以及RowStateFilter 属性。RowFilter属性可以利用比较运算子「<」、「>」、「<=」、「>=」以及「Like」来过滤记录中的数据,其语法如下所示: DataView.RowFilter = "条件叙述" 下列范例设定DefaultView 的RowFilter 属性,只有台北县的记录会被显示出来: <%@Import Namespace=System.Data.ADO%> <%@Import Namespace=System.Data%> <Script Language="VB" Runat="Server"> Sub Page_Load(Sender As Object, e As EventArgs) Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb" Dim strComStr As String = "Select * From Members" Dim dscA As ADODataSetCommand = New ADODataSetCommand(strComStr, strConStr) Dim dsDataSet As DataSet = New DataSet() dscA.FillDataSet(dsDataSet, "Members") Dim dtTable As DataTable = dsDataSet.Tables("Members") Dim shtR As Short dtTable.DefaultView.RowFilter ="UserAdd Like 台北县%" Response.Write("住台北县的总共有" & _ dtTable.DefaultView.Count.ToString & " 人, 分别是: ") For shtR=0 To dtTable.DefaultView.Count-1 显示DataView 整理的 资料 Response.Write(dtTable.DefaultView(shtR)("UserName") & " ") Next End Sub </SCRIPT> 而RowStateFilter 属性则是以记录的状态来作筛选的条件。其使用语法如下所视: DataView.RowStateFilter = DataViewState.状态 可过滤的字段的状态如下表所示: 下列范例中删除了一笔记录,并且利用RowStateFilter 属性显示这笔被删除的记录: <%@Import Namespace=System.Data.ADO%> <%@Import Namespace=System.Data%> <Script Language="VB" Runat="Server"> Sub Page_Load(Sender As Object, e As EventArgs) Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb" Dim strComStr As String = "Select * From Members" Dim dscA As ADODataSetCommand = New ADODataSetCommand(strComStr, strConStr) Dim dsDataSet As DataSet = New DataSet() dscA.FillDataSet(dsDataSet, "Members") Dim dtTable As DataTable = dsDataSet.Tables("Members") 为了方便使用 dtTable.Rows(0).Delete() 删除DataTable 中的第一笔记录 Dim shtR As Short dtTable.DefaultView.RowStateFilter = DataViewRowState.Deleted Response.Write("被删除的使用者总共有" & _ dtTable.DefaultView.Count.ToString & " 人, 分别是: ") For shtR=0 To dtTable.DefaultView.Count-1 显示DataView 整理的资 料 Response.Write(dtTable.DefaultView(shtR)("UserName") & " ") Next End Sub </SCRIPT> 上述程序虽然删除了DataTable 中的第一笔记录,除非使用DataSetCommand 对象的Update方法将所作的更动更新回数据源,否则是不会影响数据源的数据状态。 搜寻数据 要搜寻DataTable 里面的数据,可以利用DataView 的Find 方法。如果Find 方法有找到符合的数据,则传回数据所在记录的Index 值;倘若没找到则传回-1。其使用语法如下所示: 变数= DataView.Find("要搜寻的字符串") 列范例可以在文字输入盒中输入数据,按下确定后利用Find 方法找寻使用者的数据,并显示数据所在记录的Index 值: <%@Import Namespace=System.Data.ADO%> <%@Import Namespace=System.Data%> <Form Id="Form1" Runat="Server"> 请输入所要查询的数据: <Input Type="Text" Runat="Server" Id="Text1"> <Button Id="Button1" Runat="Server" OnServerClick="Button1_Click"> 查询</Button> </Form> <Span Id="Sp1" Runat="Server"/> <Script Language="VB" Runat="Server"> Sub Button1_Click(Sender As Object, e As EventArgs) Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb" Dim strComStr As String = "Select * From Members" Dim dscA As ADODataSetCommand = New ADODataSetCommand(strComStr, strConStr) Dim dsDataSet As DataSet = New DataSet() dscA.FillDataSet(dsDataSet, "Members") Dim dtTable As DataTable = dsDataSet.Tables("Members") dtTable.DefaultView.Sort="UserId" 必须要指定Sort 属性才可以搜 寻 Dim shtR As Short shtR=dtTable.DefaultView.Find(Text1.Value) 传回符合记录的 Index 值 If shtR = -1 Then 如果没找到就传回-1 Sp1.InnerText="没找到您所输入的数据" Else Sp1.InnerText="您所要找的资料在第" & shtR.ToString & " 笔记录" End if End Sub </SCRIPT> 产生自订的DataView 对象 如果一个DataView 对象不能满足我们的需求,我们还可以建立多个DataView 对象来制定数据的显示格式。其宣告语法如下所示: Dim 变量As DataView = New DataView(数据表) 下列范例中从同一个DataTable 中产生了两个自订的DataView 对象,并指定不同的数据排序方式: <%@Import Namespace=System.Data.ADO%> <%@Import Namespace=System.Data%> <Script Language="VB" Runat="Server"> Sub Page_Load(Sender As Object, e As EventArgs) Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb" Dim strComStr As String = "Select * From Members" Dim dscA As ADODataSetCommand = New ADODataSetCommand(strComStr, strConStr) Dim dsDataSet As DataSet = New DataSet() dscA.FillDataSet(dsDataSet, "Members") Dim dv1st As DataView = New DataView(dsDataSet.Tables(0)) Dim dv2nd As DataView = New DataView(dsDataSet.Tables(0)) dv1st.Sort = "UserId ASC" 指定依UserId 由小排到大的配置 dv2nd.Sort = "UserId DESC" 指定依UserId 由大排到小的配置 Dim shtR As Short Response.Write("dv1st 指定依UserId 由小排到大的配置:<br>") For shtR=0 To dv1st.Count-1 显示DataView 整理的资料 Response.Write("顺序" & (shtR+1).ToString & " " & _ " -> " & dv1st(shtR)("UserId") & " ") Next Response.Write("<p>dv2nd 指定依UserId 由大排到小的配置:<br>") For shtR=0 To dv2nd.Count-1 显示DataView 整理的资料 Response.Write("顺序" & (shtR+1).ToString & " " & _ " -> " & dv2nd(shtR)("UserId") & " ") Next End Sub </SCRIPT> 我们可以将DataView 直接当成DataTable 来使用,因为DataView 的数据来源还是DataTable对象,所以我们对DataView 对象所执行的任何操作都会影响原来的DataTable 中的数据。下列范例显示修改DataView 中的数据对原来DataTable 对象的影响: <%@Import Namespace=System.Data.ADO%> <%@Import Namespace=System.Data%> <Script Language="VB" Runat="Server"> Sub Page_Load(Sender As Object, e As EventArgs) Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb" Dim strComStr As String = "Select * From Members" Dim dscA As ADODataSetCommand = New ADODataSetCommand(strComStr, strConStr) Dim dsDataSet As DataSet = New DataSet() Dim shtR As Short dscA.FillDataSet(dsDataSet, "Members") Dim dv1st As DataView = New DataView(dsDataSet.Tables(0)) Dim dv2nd As DataView = New DataView(dsDataSet.Tables(0)) dv1st.Sort = "UserId ASC" 指定依UserId 由小排到大的配置 dv2nd.Sort = "UserId DESC" 指定依UserId 由大排到小的配置 dv1st(0)("UserTEL")="1234567890" 修改其中一个DataView 的内容 Response.Write("观察原来DataTable 中的数据是否有改变:<br>") For shtR=0 To dsDataSet.Tables(0).Rows.Count-1 DataTable 中的原来数 据 Response.Write("顺序" & (shtR+1).ToString & " " & _ " -> " & dsDataSet.Tables(0).Rows(shtR)("UserId") & " ") Next For shtR=0 To dsDataSet.Tables(0).Rows.Count-1 显示UserTel 字段的资 料 Response.Write("<br>内容" & (shtR+1).ToString & " " & _ " -> " & dsDataSet.Tables(0).Rows(shtR)("UserTel")) Next Response.Write("<p>dv1st 指定依UserId 由小排到大的配置:<br>") For shtR=0 To dv1st.Count-1 显示DataView 整理的资料 Response.Write("顺序" & (shtR+1).ToString & " " & _ " -> " & dv1st(shtR)("UserId") & " ") Next For shtR=0 To dv1st.Count-1 显示UserTel 字段的资料 Response.Write("<br>内容" & (shtR+1).ToString & " " & _ " -> " & dv1st(shtR)("UserTel")) Next Response.Write("<p>dv2nd 指定依UserId 由大排到小的配置:<br>") For shtR=0 To dv2nd.Count-1 显示DataView 整理的资料 Response.Write("顺序" & (shtR+1).ToString & " " & _ " -> " & dv2nd(shtR)("UserId") & " ") Next For shtR=0 To dv2nd.Count-1 显示UserTEL 字段的资料 Response.Write("<br>内容" & (shtR+1).ToString & " " & _ " -> " & dv2nd(shtR)("UserTEL")) Next End Sub </SCRIPT> 上列范例码修改了dv1st 这个DataView 对象中第一笔记录的数据,其修改会影响原数据源;由于dv2nd 这个DataView 的资料来源也是从原DataTable 中取得,故dv2nd 的资料显示也受影响。