Link to home
Start Free TrialLog in
Avatar of BKennedy2008
BKennedy2008

asked on

Remove row in datagridview if row index contains in a string

I am cycling through a datgridview row to check for duplicates on Badge Numbers, "Clocked  IN" as well as datetime.
I would like to remove the rows once it finds duplicates, but for now, I add the row.index to a string and have a string "1, 3, 5"
Then recycle through the rows and remove the index after I break up the string.
Is there a more efficient way in this approach?

 Dim RowCount As String = ""
        For i = 0 To Me.DataGridView2.RowCount - 1
            If DataGridView2.Rows(i).Cells(8).Value = "Clocked IN" Then

                Dim LID As String = DataGridView2.Rows(i).Cells(0).Value
                Dim jb As String = DataGridView2.Rows(i).Cells(1).Value
                Dim BDG As String = DataGridView2.Rows(i).Cells(3).Value
                Dim CStat As String = DataGridView2.Rows(i).Cells(8).Value
                Dim Date1 As Date = DataGridView2.Rows(i).Cells(7).Value
                Dim d2 As String = Date1.ToString("MM/dd/yy")
                Dim j1 As Integer = Me.DataGridView2.RowCount - 1
                For Each row As DataGridViewRow In DataGridView2.Rows


                    If ((row.Cells(3).Value)) = BDG Then  ' Badge Numbers Agree

                        If (row.Cells(8).Value) = "Clocked OUT" Then ' Status is Clocked Out
                            Dim date2 As Date = row.Cells(7).Value ' Set Date2 to convert
                            If (date2.ToString("MM/dd/yy")) = d2 Then   ' Date Values equal out
                                If (row.Cells(1).Value) = jb Then 'Job Numbers equal out
                                    'If (row.Cells(8).Value) = "Clocked IN" Then

                                    DataGridView2.Rows(i).Cells(10).Value = row.Cells(7).Value ' Set ClockOut to the ScanTime
                                   RowCount = RowCount + row.Index.ToString + ","  ' Add row to remove from gridview as the clock


                                End If

                            End If


                        End If

                    End If


                Next
            End If
        Next
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

How is the data getting to the DataGridView?  Off the cuff, sounds like you should be cleaning up your results in your select statement.
Avatar of BKennedy2008
BKennedy2008

ASKER

I am using a SQL view to gather the data, and would have to match that data up as well. I didn't have time to write the SQL View, but the native SQL View is


SELECT     dbo.Tbl_ScannedLabor.EntryID, dbo.Tbl_ScannedLabor.Job#, dbo.tbl_JCJobs.JobName, dbo.Tbl_ScannedLabor.Badge#, dbo.LaborRoster.Name, dbo.SubcontractorRates.Company,
                      dbo.Tbl_ScannedLabor.LaborClass, dbo.Tbl_ScannedLabor.ScannedTime, CASE WHEN (dbo.Tbl_ScannedLabor.ClockInOut)
                      = 'True' THEN 'Clocked IN' ELSE 'Clocked OUT' END AS ClockedStatus
FROM         dbo.LaborRoster INNER JOIN
                      dbo.SubcontractorRates ON dbo.LaborRoster.CompanyID = dbo.SubcontractorRates.CompanyID INNER JOIN
                      dbo.Tbl_ScannedLabor INNER JOIN
                      dbo.tbl_JCJobs ON dbo.Tbl_ScannedLabor.Job# = dbo.tbl_JCJobs.Job# ON dbo.LaborRoster.Badge = dbo.Tbl_ScannedLabor.Badge#
WHERE     (dbo.Tbl_ScannedLabor.IsDeleted = 0)
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FYI you can use the 'CODE' format to wrap your code in a text block to make it more readable.
You can modify this logic as per your need
String searchValue = "somestring";
int rowIndex = -1;
foreach(DataGridViewRow row in DataGridView1.Rows)
{
    if(row.Cells[1].Value.ToString().Equals(searchValue)) //search value will be you required string that is getting from your query
    {
        rowIndex = row.Index;
       DataGridView1.Rows.Remove(DataGridView1.Rows[rowIndex ]);
        break;
    }
}

Open in new window

Thanks Dustin, I took your advice, and instead of trying to manipulate data in a gridview, its best to do it at the SQL level.