DataTable RowFilter 过滤数据


用Rowfilter加入过滤条件

eg:

string sql = "select Name,Age,Sex from UserInfo";

DataTable dt = DataAccess.GetDataTable(sql);//外部方法(通过一条查询语句返回一个DataTable)

dt.DefaultView.RowFilter = "Sex=女";

dt.DefaultView.RowFilter = "Age>18";

dt.DefaultView.RowFilter = "Name like '%张%'";

不过RowFilter不支持不等于(<>、!=、not like),不过如果只是单纯的对确定的字符串操作,可以用in和not in,数据库查询语句则不行。

 

Dim dt As DataTable = Getdata(20078, "abc")
Dim view As DataView = New DataView(dt)
view.RowFilter = "Names in('ttt','tttt')"
GridView1.DataSource = view.ToTable() --这个才过滤成功 
GridView1.DataBind()

 


Private Function GetNewTable(ByVal dt As DataTable, ByVal filter As String) As DataTable
Dim newTable As DataTable = dt.Clone()
Dim drs As DataRow() = dt.Select(filter)
For Each dr As DataRow In drs
Dim arr As Object() = dr.ItemArray
Dim newrow As DataRow = newTable.NewRow()
For i As Integer = 0 To arr.Length - 1
newrow(i) = arr(i)
Next
newTable.Rows.Add(newrow)
Next
Return newTable
End Function

 datatable 中select在vb.net中写法

ds.Tables("RoomType").Select("MealType=" & "'" & mgdr("MealType") & "' and Roomtypecode=" & "'" & mgdr("Roomtypecode") & "' and Availability=" & "'" & mgdr("Availability") & "' and VendorCurreny=" & "'" & mgdr("VendorCurreny") & "' and RoomAdults=" & "'" & mgdr("RoomAdults") & "' and FromDate=" & "'" & mgdr("FromDate") & "' and ToDate=" & "'" & mgdr("ToDate") & "' and Runno=" & "'" & mgdr("Runno") & "'")

也可以用String.Format 格式化

 

data.Tables("error").Columns.Remove("postXml")'移除某列数据
GridView2.DataSource = data.Tables("error")
GridView2.DataBind()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM