Datatable Sorting From A Custom Number Sequence - vb.net

My datatable has a field that has values like so..
0000006685-001
0000006685-002
0000006713-001
0000006714-002
0000006713-002
0000006697-002
I want the datatable to be re-ordered by this field so the numbers on the left of the hyphen go in order, followed by the corresponding sequence in order. So the above example data would be ordered like..
0000006685-001
0000006685-002
0000006697-002
0000006713-001
0000006713-002
0000006714-002

Dim dv As New DataView(parsedDataset.Tables("Detail"))
dv.Sort = "the field"
Dim dt As DataTable = dv.ToTable

The easiest way is, most probably, through the Select method then the CopyToTable method
dt = dt.Select("", "ColumnName ASC").CopyToDataTable
Replace ColumnName with the name of the column for that field.

Related

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

Retrieve the latest or the last value of a column in a dataset

The below code gives the first row.. but i need to get the latest or the last row updated. Please help
Dim dt As DateTime = ds.Tables(0).Rows(0)("Columnname")
You can use the Rows.Count property as shown in other answer or just let do that to Linq
Dim row = ds.Tables(0).AsEnumerable().Last()
Dim dt As DateTime = row.Field(Of DateTime)("ColumnName")
Of course this works for the last row of the table. This doesn't mean something like the last (more recent) value for the "ColumnName". If this is your intention then you need to "Sort" the datatable or better ask the source (a database ? ) of the rows to sort it.
If you are not able to change the data loading query to have it sorted directly from the database engine then you could reach (in code) the latest row ordered by "ColumnName" using something like this
' Create a dataview from the datatable, with no filter and ordered by ColumnName
Dim dv As DataView = New DataView(ds.Tables(0), "", "ColumnName ASC", DataViewRowState.CurrentRows)
dt = dv.Cast(Of DataRowView).Last().Row.Field(Of DateTime)("Column")
You have to use ds.Tables(x).Rows.Count-1
Dim dt As DateTime = ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1)("Columnname")

How to put a value from SQL number format column to a double in VB.net?

I have selected from SQL like this
SELECT MAX(receivecount) FROM config
which shows only one value of 20161.
so now I can't take that number and put it in a double or a int32. The column name is ReceiveCount
Current what I have is this
Dim ads2 As New DataTable
strrs2 = "SELECT MAX(recievecount) FROM tblcmconfig"
Using ads23 As OracleDataReader = GetDataReader(strrs2, CommandType.Text, ExecuteType.Execute)
ads2.Load(ads23)
End Using
Dim dugaar As Double '= Convert.ToDouble(ids)
dugaar = Double.Parse(ads2("recievecount").ToString)
After this I am trying to put that value in a textedit.editvalue as that textedit is numbers only enterable.
Assuming your datatable is populated correctly. You either need to give the MAX(recievecount) a name in the select statement like:
MAX(recievecount) as rc
Then
Double.Parse(ads2(0)("rc").ToString)
Or you can refer to the row and and column numers:
Double.Parse(ads2(0)(0).ToString)

VB.NET delete empty datarow

For Each dr In ds.Tables(0).Rows
If String.IsNullOrEmpty(dr("BIL")) Then
dr.Delete() //how to delete this row?
End If
Next
first,will loop all data then check which row in BIL column are empty,if the row in BIL column are empty,then delete the row from dataset,how to delete this empty datarow?
Do you want to delete it in your database or do you want to remove it from the DataTable? Btw, use dr.IsNull("BIL") instead. Your code compiles only because you've set OPTION STRICT off because dr("BIL") returns object instead of string.
Dataset are getting data from EXCEL,so,dont have any identity
column.BTW i just want remove from datatable, not database
Then you have to use DataRowCollection.Remove instead of DataRow.Delete. With Delete wthe row will change it's RowState to Deleted. If you then use a DataAdapter to update the DataSet/DataTable or DataRow it will be deleted in the database.
But you can also use Linq-To-DataSet to filter the table and use DataRow.Field extension method which is strongly typed and supports nullable types:
Dim notNullBilRows = From row In ds.Tables(0)
Where Not String.IsNullOrEmpty(row.Field(Of String)("BIL"))
Now you can use CopyToDataTable to create a new DataTable with only rows where BIL is not null, which seems to be the actual requirement.
Dim tblNotNullBilRows = notNullBilRows.CopyToDataTable()
Here's the non-Linq approach with Remove, you have to create an intermediate collection since you cannot remove elements from a collection during enumeration:
Dim removeList = New List(Of DataRow)
For Each dr As DataRow In ds.Tables(0).Rows
If String.IsNullOrEmpty(dr.Field(Of String)("BIL")) Then
removeList.Add(dr)
End If
Next
For Each dr As DataRow In removeList
ds.Tables(0).Rows.Remove(dr)
Next
Try this:
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
If String.IsNullOrEmpty(dt.Rows(i)("BIL")) Then
dt.Rows.RemoveAt(i)
End If
Next
You will want to put the index of the rows you wish to delete into an array, then iterate through the array deleting each rows from the datatable using the indexes. You will not need an 'identity' column to do this, the rows will automatically be asigned indexes.
Assuming you have 2 columns in table tbl: ColumnA and ColumnB
Dim dv as new DataView
dv = new DataView(tbl)
dv.RowFilter = "ColumnA <> '' AND ColumnB <> ''"
tbl = dv.ToTable()
tbl should no longer have empty rows. Hope this helps.

DataTable.Select with converting string value to Date format and sort it

I have a data table that is being returned by a 3rd party component, so I have no way of changing the SQL string to get what I want. So, I get a table with several columns, all of them are Strings. One of the columns is BILLDATE which is of type string, but actually holds a date in a MM/DD/YYYY format.
Issue: I need to sort the records in descending order and pick the record with the biggest date.
CODE:
Dim dataRows As DataRow()
Dim dt As New DataTable
dt = GetTable()
dataRows = dt.Select("", "BILLDATE DESC")
Sample data:
10/23/2010
9/23/2010
8/23/2010
7/23/2010
6/23/2010
From the sample data, the 9/23/2010 record is returned as the first record, not the 10/23/2010.
Here is what I tried:
dataRows = dt.Select("MAX(CONVERT(DateTime,BILLDATE))", "") - run-time Error
dataRows = dt.Select("", "Convert(BILLDATE,'System.DateTime')") - run-time Error
I would prefer not to iterate through all the records to get the latest date and select it. Any ideas?
EDIT 1 - 2012-12-07 4:42pm:
Added definition for dataRows. It is of type DataRow()
You can use DateTime.ParseExact with multiple format strings. You need them since your "dates" sometimes have one and sometimes have two places. I would use Linq instead:
Dim ordered = From row In dt.AsEnumerable()
Let billDate = Date.ParseExact(
row.Field(Of String)("Billdate"),
{"MM/dd/yyyy", "M/dd/yyyy"},
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None)
Order By billDate Descending
Select row
' if you want to create a new DataTable from the rows: '
Dim tblOrdered = ordered.CopyToDatatable()
Dim LastDate As Date = dt.AsEnumerable().Max(Function(a) Convert.ToDateTime(a("BILLDATE")))