Dataview filtering vb.net - vb.net

Wondering if I am doing this filter right with the dataview, it keeps throwing me this error
Additional information: Filter expression '80' does not evaluate to a Boolean term.
but here is the code
Dim table = DataSet1.Tables("network")
table.DefaultView.RowFilter = TextBox1.Text
DataGridView1.DataSource = table

To filter something on a DataVIew you need to specify the column on which the filter should be applied, the operator for the comparison and the value that you want to use for the filtering. It seems that you have given only the value "80".
For example, assuming the column of interest is named "NumberOfPieces" and you have typed 80 in the textbox
Dim table = DataSet1.Tables("network")
table.DefaultView.RowFilter = "NumberOfPieces = " & TextBox1.Text
DataGridView1.DataSource = table
This will filter the view with all the rows that have the value (a numeric value) equals to 80 in the column "NumberOfPieces". You could use other operators like Greater/Lesser Than ( >= <= ) or more complex construct that are well detailed in the MSDN page about the Expression property of the DataColumn object

Related

MS-Access How to use iif in query criteria

I have a search form containing a text box and I want to filter a field based on value greater than value entered in text box but return all the field when the text box is empty.
I wrote below code in query criteria for this purpose but got error
IIf(IsNull(TextBox); TheField; >TextBox))
When specified as the criteria for a particular field within the query builder in MS Access, the expression you have entered will likely be interpreted as the following where clause:
where TheField = IIf(IsNull(TextBox), TheField, TheField > TextBox))
Which will likely yield a data type mismatch error if TheField does not hold a boolean value, since the else branch will yield a where clause of:
where TheField = (TheField > TextBox)
Which will be either of the following:
where TheField = True
where TheField = False
Instead, I would suggest specifying the following as your SQL where clause:
where Forms!YourFormName!YourTextBox is null or TheField > Forms!YourFormName!YourTextBox
I would recommend:
where (textbox is null or thefield > textbox)

iam trying to get datable specific columns which has null value using linq in vb.net get only those 2 empty rows

When I check the column values in a DataTable using LINQ, they are not equal. Here is the code I'm using which has the problem:
for eg if columnA 5 rows which two or empty ir null i should
DT.
Select("Source <> ('AMEX','VISA')").
ToList().
ForEach(Sub(row) row(ColumnExpType)="T")
To get all the rows, using LINQ, which have NULL in ColumnA you could do something like this:
DT.Rows().
Cast(Of DataRow).Where(Function(row) row("ColumnA") Is DBNull.Value)
However, depending on your needs, you may prefer to use the old Select method:
DT.Select("ColumnA IS NOT NULL")
If you also need to check if the column contains an empty string, then to do that with Select:
DT.Select("ColumnA IS NOT NULL OR ColumnA = ''")
But, if you want to use LINQ, then it depends on the type of the column and whether it's a typed DataTable (a table from a typed DataSet). If the column is a standard string type, like NVARCHAR in T-SQL, and the DataTable is just a standard non-typed DataTable, then you could do something like this:
DT.Rows().
Cast(Of DataRow).
Where(Function(row) row("ColumnA") Is DBNull.Value Or DirectCast(row("ColumnA"), String) = "")
Select("Source <> ('AMEX','VISA')") will not work.
You need
Select("Source <> 'AMEX' And Source <> 'VISA'")
Not sure what the For Each is doing. This is a working example of using my test database.
Dim lst = dt.Select("Rating <> 'Unrated' And Rating <>'Good'").ToList
For Each dr As DataRow In lst
Debug.Print(dr("Name").ToString)
Next

VB.Net DataTable.Select - Sort expression syntax

I need to select rows from a DataTable and sort them by "Field1 / Field2"
I tried this code:
Using DT_Tmp As DataTable = DT.Select("", "FirstNum/SecondNum desc").CopyToDataTable
but I get error saying that column "FirstNum/SecondNum" doesn't exist.
EDIT
So far I'm using (as a workaround) a temp table which I'm adding a Field where I store the ratio and I'm using that Field to sort the table.
I don't think the select method is smart enough to do calculations on the field. The other workaround is to add a computed column to your datatable and use that as the sort column
Try putting each column inside brackets
Using DT_Tmp As DataTable = DT.Select("", "[FirstNum] / [SecondNum] desc").CopyToDataTable
Based on the links you looked at, you need to add the caluculated column first.
csortnum= New DataColumn
With csortnum
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "sortcolumn"
.Expression = "FirstNum/SecondNum"
End With
DT.add(csortnum)
Using DT_Tmp As DataTable = DT.Select("", csortnum desc").CopyToDataTable

Filtering a DataGridView Twice

I have a DataGridView with a bindingsource from an Access table. I am using this code:
QrysbfrmPupilsAndExamsBindingSource.Filter = "CandNumber = " & TextBox1.Text
to filter the datagridview by the column CandNumber. I then want to filter it again, keeping the results from the previous filter, not filtering from the full access table again.
When you bind a DataTable to a BindingSource, you can use an expressions using the syntax described within the DataColumn.Expression property.
So if you e.g. want to filter by an additional column, just use And:
yourBindingSource.Filter = String.Format("CandNumber = {0} AND Something = {1}",
TextBox1.Text, TextBox2.Text)

Rowfilter Select syntax using a value stored in a variable

I am using VB.NET and I need to use Datatable.Select to get a row which has a matching value.
Consider I have Datatable_A, which has many rows. I am interested to get row/rows which matches my search criteria. In this case, I must get a row (RowA), which has AnswerA stored in Column_A.
I know I can easily use the method below to find the answer:
RowA = Datatable_A.Select("Column_A = 'AnswerA'")
However, value 'AnswerA' is stored in Variable_A.
I have tried using
RowA = Datatable_A.Select("Column_A = 'Variable_A'")
Unfortunately, I still could not get the datarow, whose Column_A = 'Answer A'.
I have studied the explanation in this website DataView RowFilter Syntax [C#], but I could not find any clue there.
Thank you.
If you want to select a subset of your datatable rows using the Select method and a variable that contains the value to filter your rows you need to write
Dim Variable_A = "MySearchCriteria"
Dim rowsSelected = Datatable_A.Select("Column_A = '" & Variable_A & "'")
This means that you want all the rows in which the Column_A (a string column) contains exactly the value "MySearchCriteria" stored in the Variable_A. The filter condition is created concatenating the literal string describing the name of the column and the operator with the content of the Variable_A. Since Column_A is assumed to be a string column then you need to encapsulate the Variable_A between single quotes.
And remember that Select returns an array of DataRow that match the filter expression.
For Each row in rowsSelected
Console.WriteLine(row("Column_A").ToString)
Next