我有rowfilter,用于过滤我的数据源。我有几个控件,用户可以选择检查特定复选框是否用于Rowilter。下面的代码。
Dim ds = CType(dgvAbmessungen.PrimaryGrid.DataSource, DataSet)
Dim dtMain As DataTable = ds.Tables(0)
Dim filterStr As String = String.Empty
Dim byNummer As String = " [Nummer] like '" & txtFilterByNummer.Text.Trim() & "%'"
Dim byUser As String = " [User] = '" + cbUser.Text
Dim byCreateDateFrom As String = " [CreateDate] >= '" + CType(Convert.ToDateTime(calFrom.Value.Date), String)
Dim byCreateDateTo As String = "' ([CreateDate] < '" + CType(Convert.ToDateTime(calTo.Value.Date), String)
'select case checboxes and construct final rowfilter's string
dtMain.DefaultView.RowFilter = filterStr
要检查不同控件的Checbox:
for Nummer there is checkboxNummer
for User there is checkboxUser
for CreateDateFrom there is checkboxCreateDateFrom
for CreateDateTo there is checkboxCreateDateTo
目标是用户检查一个或多个checbox并应构造过滤器(字符串)。问题是我不知道如何连接&#34;和&#34;如果用户检查多个不同的checbox,则字符串之间的关键字。
目前我尝试使用精选案例。什么是最有效的方法?
答案 0 :(得分:1)
我会使用LINQ:
Dim data = dtMain.AsEnumerable()
If checkboxNummer.Checked Then
data = data.Where(Function(row) row.Field(Of String)("Nummer").Contains(txtFilterByNummer.Text.Trim()))
End If
If checkboxUser.Checked Then
data = data.Where(Function(row) row.Field(Of String)("User") = cbUser.Text.Trim())
End If
If CreateDateFrom.Checked Then
data = data.Where(Function(row) row.Field(Of Date)("CreateDate") >= calFrom.Value.Date)
End If
If CreateDateTo.Checked Then
data = data.Where(Function(row) row.Field(Of Date)("CreateDate") < calTo.Value.Date)
End If
如果您需要DataRow()
的结果,请使用data.ToArray()
。如果您需要DataTable
使用:
If data.Any() Then dtMain = data.CopyToDataTable()