请教一个DataView 和 DataSet 的问题

zwl 2006-08-22 05:33:16
在DataView中已过滤掉无用的数据,

但要求返回已过滤掉无用记录的 DataSet 怎么实现呢?

试了好多次了,请教?
...全文
256 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwl 2006-08-22
  • 打赏
  • 举报
回复
感谢 Knight94 Samen168 gezichong 三位的帮助,问题已解决

tshark 2006-08-22
  • 打赏
  • 举报
回复
VB...学习中
gezichong 2006-08-22
  • 打赏
  • 举报
回复
Private Sub GetRowsByFilter()

Dim customerTable As DataTable = New DataTable("Customers")

' Add columns
customerTable.Columns.Add("id", GetType(Integer))
customerTable.Columns.Add("name", GetType(String))

' Set PrimaryKey
customerTable.Columns("id").Unique = True
customerTable.PrimaryKey = New DataColumn() _
{customerTable.Columns("id")}

' add ten rows
Dim id As Integer
For id = 1 To 10
customerTable.Rows.Add( _
New Object() {id, String.Format("customer{0}", id)})
Next id
customerTable.AcceptChanges()

' add another ten rows
For id = 11 To 20
customerTable.Rows.Add( _
New Object() {id, String.Format("customer{0}", id)})
Next id

Dim expression As String
Dim sortOrder As String

expression = "id > 5"
' Sort descending by CompanyName column.
sortOrder = "name DESC"

' Use the Select method to find all rows matching the filter.
Dim foundRows As DataRow() = _
customerTable.Select(expression, sortOrder, _
DataViewRowState.Added)

PrintRows(foundRows, "filtered rows")

foundRows = customerTable.Select()
PrintRows(foundRows, "all rows")
End Sub

Private Sub PrintRows(ByVal rows() As DataRow, ByVal label As String)
Console.WriteLine("\n{0}", label)
If rows.Length <= 0 Then
Console.WriteLine("no rows found")
Exit Sub
End If

Dim row As DataRow
Dim column As DataColumn
For Each row In rows
For Each column In row.Table.Columns
Console.Write("\table {0}", row(column))
Next column
Console.WriteLine()
Next row
End Sub
landy_shasha 2006-08-22
  • 打赏
  • 举报
回复
学习,~~~~~~~~
Knight94 2006-08-22
  • 打赏
  • 举报
回复
to 但要求返回已过滤掉无用记录的 DataSet 怎么实现呢?

用同样的filter来进行查找,例如:
DataRow[] dr = yourDataTable.Select( yourDv.RowFilter, null, null, DataViewRowState.ModifiedCurrent );

DataTable dtClone = yourDataTable.Clone();//Clone datatable structure
DataSet ds = new DataSet();
ds.Tables.Add( dtClone );
ds.Merge( dr );
Samen168 2006-08-22
  • 打赏
  • 举报
回复
首先需要明确过滤只是对DataView有效,如果你通过table去取数据还是原来的
真的需要返回过滤后的数据只有创建一个新的DataSet把得到的行加进去了
zwl 2006-08-22
  • 打赏
  • 举报
回复
顶一下!

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧