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).Cell s(8).Value = "Clocked IN" Then
Dim LID As String = DataGridView2.Rows(i).Cell s(0).Value
Dim jb As String = DataGridView2.Rows(i).Cell s(1).Value
Dim BDG As String = DataGridView2.Rows(i).Cell s(3).Value
Dim CStat As String = DataGridView2.Rows(i).Cell s(8).Value
Dim Date1 As Date = DataGridView2.Rows(i).Cell s(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).Cell s(10).Valu e = 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
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).Cell
Dim LID As String = DataGridView2.Rows(i).Cell
Dim jb As String = DataGridView2.Rows(i).Cell
Dim BDG As String = DataGridView2.Rows(i).Cell
Dim CStat As String = DataGridView2.Rows(i).Cell
Dim Date1 As Date = DataGridView2.Rows(i).Cell
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"
If (row.Cells(1).Value) = jb Then 'Job Numbers equal out
'If (row.Cells(8).Value) = "Clocked IN" Then
DataGridView2.Rows(i).Cell
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
How is the data getting to the DataGridView? Off the cuff, sounds like you should be cleaning up your results in your select statement.
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.Entry ID, dbo.Tbl_ScannedLabor.Job#, dbo.tbl_JCJobs.JobName, dbo.Tbl_ScannedLabor.Badge #, dbo.LaborRoster.Name, dbo.SubcontractorRates.Com pany,
dbo.Tbl_ScannedLabor.Labor Class, dbo.Tbl_ScannedLabor.Scann edTime, CASE WHEN (dbo.Tbl_ScannedLabor.Cloc kInOut)
= 'True' THEN 'Clocked IN' ELSE 'Clocked OUT' END AS ClockedStatus
FROM dbo.LaborRoster INNER JOIN
dbo.SubcontractorRates ON dbo.LaborRoster.CompanyID = dbo.SubcontractorRates.Com panyID 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.IsDe leted = 0)
SELECT dbo.Tbl_ScannedLabor.Entry
dbo.Tbl_ScannedLabor.Labor
= 'True' THEN 'Clocked IN' ELSE 'Clocked OUT' END AS ClockedStatus
FROM dbo.LaborRoster INNER JOIN
dbo.SubcontractorRates ON dbo.LaborRoster.CompanyID = dbo.SubcontractorRates.Com
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.IsDe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
ASKER
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.