i'm in VS2008 Studio, i have this datagridview with multiple columns which the last column contains a date and time value.
lot's of rows are pretty the same except by they're date column.
what i wanted to do is to trim the whole datagridview duplicate rows except they're most recent ones based on they're date column.
i have sth like this:
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 23:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 21:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 22:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 20:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 11:11:59
Everyone ,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 17:11:59
Everyone ,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 14:11:59
the output i want should be like this:
Administrator 192.168.137.221 2 file://C:\WMPub\WMRoot\industrial.wmv 07.Jul.2014 - 23:11:59
Everyone 192.168.137.201 2 file://C:\WMPub\WMRoot\industrial.wmv 07.Jul.2014 - 17:11:59
please consider "," as column seprators! (i don't know how to draw a table here, sorry again)!
i have this code but it throw a nullreferenceexception at the If dtRow("dateColumn") >.... and if i sort my last column and then remove the check inside this code it make a mess: before: i61.tinypic.com/2s1wrdj.jpg and after trimming: i62.tinypic.com/ogwizq.jpg
and i use this function like this:
this is the whole routine:
lot's of rows are pretty the same except by they're date column.
what i wanted to do is to trim the whole datagridview duplicate rows except they're most recent ones based on they're date column.
i have sth like this:
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 23:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 21:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 22:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 20:11:59
Administrator,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 11:11:59
Everyone ,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 17:11:59
Everyone ,192.168.137.221,2,file://C:\WMPub\WMRoot\industrial.wmv , 07.Jul.2014 - 14:11:59
the output i want should be like this:
Administrator 192.168.137.221 2 file://C:\WMPub\WMRoot\industrial.wmv 07.Jul.2014 - 23:11:59
Everyone 192.168.137.201 2 file://C:\WMPub\WMRoot\industrial.wmv 07.Jul.2014 - 17:11:59
please consider "," as column seprators! (i don't know how to draw a table here, sorry again)!
i have this code but it throw a nullreferenceexception at the If dtRow("dateColumn") >.... and if i sort my last column and then remove the check inside this code it make a mess: before: i61.tinypic.com/2s1wrdj.jpg and after trimming: i62.tinypic.com/ogwizq.jpg
Public Function RemoveDuplicateRows(ByVal dTable As DataTable, ByVal colName As String)Dim dict As New dictionary(Of String, DataRow) For Each dtRow As DataRow In dTable.Rows Dim key As String = dtRow("column1") + "," + dtRow("column2") ' + etc. Dim dictRow As DataRow = Nothing If dict.TryGetValue(key, dictRow) Then 'check and update date 'you can skip this part, if your data is sorted If dtRow("dateColumn") > dictRow("dateColumn") Then dictRow("dateColumn") = dtRow("dateColumn") End If Else dict.Add(key, dictRow) End If Next Return dict.Values.ToArray() End Function
and i use this function like this:
DataGridView1.DataSource = RemoveDuplicateRows(DataGridView1.DataSource, "Lastseen")
this is the whole routine:
Last edited: