|
-
Dec 11th, 2020, 11:27 PM
#1
Thread Starter
New Member
[RESOLVED] In Vb.net I need to add a message box to current code with Red backcolor
I have a button that retrieves data and place the information in data grid, its currently working but I need to add a message box to it. If the back color in the grid is red when the button is click a message box pop up "Correct all red fields before continuing." I have tried to insert message boxes to test but in the for each loop the message box appeared for each red back color cell and that's not what I need it to do. I need it to search the entire grid for for red back color and display the message box.
Here is the code:
Code:
Private Sub Retrieve()
On Error GoTo errH
Dim con As OdbcConnection
Dim cmd As OdbcCommand
Dim dr As OdbcDataReader
Dim strItem1 As String
Dim strItem2 As String
Dim strOdbc As String = ""
Dim hasRows As Boolean = False
Dim strItem3 As String
Dim strRelease As String
Dim sqlStr As String
Dim strDsn, strSystem As String
strDsn = ComboBox1.Text
strSystem = txtSystem.Text
strOdbc = "{iSeries As ODBC Driver};System=" + strSystem + ";Dsn=" + strDsn + ";Uid=TEST!;Pwd=Test;"
con = New OdbcConnection(strOdbc)
con.Open()
Using con
For Each dgvRow As DataGridViewRow In gridUserEntries.Rows
If (Not IsNothing(dgvRow.Cells(0))) Then
If (Not (IsNothing(dgvRow.Cells(0).Value))) Then
strRelease = dgvRow.Cells(0).GetEditedFormattedValue(dgvRow.Cells(0).RowIndex, DataGridViewDataErrorContexts.Display)
sqlStr = "Select * from jobscopedb.ppusrfs where search_key_uf= '" + strRelease + "'"
cmd = New OdbcCommand(sqlStr, con)
'con.Open()
dr = cmd.ExecuteReader()
hasRows = dr.HasRows
While (hasRows And dr.Read)
strRelease = dr.Item(0).ToString
strItem2 = dr.Item(2).ToString
strItem3 = dr.Item(3).ToString
If (strItem2.Contains("27 PRODWK")) Then
dgvRow.Cells(1).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("28 SHIPMON")) Then
dgvRow.Cells(2).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("30 %COMPL")) Then
dgvRow.Cells(3).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("31 TGTSHIP")) Then
dgvRow.Cells(4).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("70 SCHPROD")) Then
dgvRow.Cells(5).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("81 AB%")) Then
dgvRow.Cells(6).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("82 ARM%")) Then
dgvRow.Cells(7).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("83 SHAFT%")) Then
dgvRow.Cells(8).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("84 FIT% ")) Then
dgvRow.Cells(9).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("85 HDW%")) Then
dgvRow.Cells(10).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("86 FIN% ")) Then
dgvRow.Cells(11).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("87 WELDCMP")) Then
dgvRow.Cells(12).Value = dr.Item(3).ToString.Trim
End If
End While
cmd.Dispose()
End If
End If
Next
End Using
Exit Sub
errH:
MsgBox(Err.Description)
con = Nothing
End Sub
-
Dec 12th, 2020, 04:57 AM
#2
Re: In Vb.net I need to add a message box to current code with Red backcolor
I have read the question several times now and it's not exactly clear what problem you're having. Could you take your time and write a more clear explanation. It reads like it was written in a hurry. I think I know what you're asking but I don't want to make assumptions. It just wastes everyone's time when the assumption is wrong.
-
Dec 12th, 2020, 09:08 PM
#3
Re: In Vb.net I need to add a message box to current code with Red backcolor
Yeah, I don't see where you check the cell backcolor and display a messagebox anywhere in the code you post. But if you want to check all cells in a datagridview then
Code:
For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
For Each c As DataGridViewCell In row.Cells
MessageBox.Show(c.Style.BackColor.ToString)
Next
Next
End Sub
-
Dec 12th, 2020, 10:34 PM
#4
Thread Starter
New Member
Re: In Vb.net I need to add a message box to current code with Red backcolor
 Originally Posted by wes4dbt
Yeah, I don't see where you check the cell backcolor and display a messagebox anywhere in the code you post. But if you want to check all cells in a datagridview then
Code:
For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
For Each c As DataGridViewCell In row.Cells
MessageBox.Show(c.Style.BackColor.ToString)
Next
Next
End Sub
When I put this code in I get the message box for ever red back color field in the grid. How do I get it to display only one time if there is red back color in the grid?
-
Dec 12th, 2020, 10:37 PM
#5
Re: In Vb.net I need to add a message box to current code with Red backcolor
Set a boolean in the loop, and then show the messagebox after the loop if the boolean is true.
But I would also exit the loop as soon as the first red background cell is found, since you don't care if there is one or multiple, no point in continuing to search once you find the first one.
Last edited by passel; Dec 12th, 2020 at 10:41 PM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Dec 12th, 2020, 10:39 PM
#6
Re: In Vb.net I need to add a message box to current code with Red backcolor
Code:
Dim found As Boolean = False
For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
For Each c As DataGridViewCell In row.Cells
If c.Style.BackColor = Color.Red Then found = True
Next
Next
If found Then
MessageBox.Show("found red cell")
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 12th, 2020, 10:49 PM
#7
Re: In Vb.net I need to add a message box to current code with Red backcolor
 Originally Posted by .paul.
Code:
Dim found As Boolean = False
For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
For Each c As DataGridViewCell In row.Cells
If c.Style.BackColor = Color.Red Then found = True
Next
Next
If found Then
MessageBox.Show("found red cell")
End If
Another option that will terminate the loops once the first red cell is found:
Code:
Dim found As Boolean = False
For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
For Each c As DataGridViewCell In row.Cells
If c.Style.BackColor = Color.Red Then
found = True
MessageBox.Show("found red cell")
Exit For
End If
Next
If found = True Then
Exit For
End If
Next
-
Dec 12th, 2020, 10:54 PM
#8
Thread Starter
New Member
Re: In Vb.net I need to add a message box to current code with Red backcolor
I have created a program that have a Datagridview and buttons (Clear, Retrieve, and Update). The first column in the grid (column 0) is called Release . End users enter release numbers in this column and tab (Cell leave) , it then validates if the release is in the database. If the release is valid the back color is white, if the release is invalid the back color turns red. The code I provided is for the retrieve button. Currently when you click the retrieve button and you have valid and invalid releases it pulls the valid release data and leaves the invalid with the red back color blank. I would like if there are red back color fields to populate a message box and not return data for the valid release until the invalid is corrected .
-
Dec 12th, 2020, 11:43 PM
#9
Re: In Vb.net I need to add a message box to current code with Red backcolor
 Originally Posted by msboddie
I have created a program that have a Datagridview and buttons (Clear, Retrieve, and Update). The first column in the grid (column 0) is called Release . End users enter release numbers in this column and tab (Cell leave) , it then validates if the release is in the database. If the release is valid the back color is white, if the release is invalid the back color turns red. The code I provided is for the retrieve button. Currently when you click the retrieve button and you have valid and invalid releases it pulls the valid release data and leaves the invalid with the red back color blank. I would like if there are red back color fields to populate a message box and not return data for the valid release until the invalid is corrected .
To help more with that more specific issue, we need to see the relevant code...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 13th, 2020, 12:17 AM
#10
Thread Starter
New Member
Re: In Vb.net I need to add a message box to current code with Red backcolor
 Originally Posted by .paul.
To help more with that more specific issue, we need to see the relevant code...
This code below is the current code that retrieves invalid (red back color) and valid release in the grid. The question I have is how do I add a message box to this code to populate a message "Please update the red fields" and not populate the valid data until the red back color is white.
Code:
Private Sub Retrieve()
On Error GoTo errH
Dim con As OdbcConnection
Dim cmd As OdbcCommand
Dim dr As OdbcDataReader
Dim strItem1 As String
Dim strItem2 As String
Dim strOdbc As String = ""
Dim hasRows As Boolean = False
Dim strItem3 As String
Dim strRelease As String
Dim sqlStr As String
Dim strDsn, strSystem As String
strDsn = ComboBox1.Text
strSystem = txtSystem.Text
strOdbc = "{iSeries As ODBC Driver};System=" + strSystem + ";Dsn=" + strDsn + ";Uid=TEST;Pwd=TEST1;"
con = New OdbcConnection(strOdbc)
con.Open()
Using con
For Each dgvRow As DataGridViewRow In gridUserEntries.Rows
If (Not IsNothing(dgvRow.Cells(0))) Then
If (Not (IsNothing(dgvRow.Cells(0).Value))) Then
strRelease = dgvRow.Cells(0).GetEditedFormattedValue(dgvRow.Cells(0).RowIndex, DataGridViewDataErrorContexts.Display)
sqlStr = "Select * from jobscopedb.ppusrfs where search_key_uf= '" + strRelease + "'"
cmd = New OdbcCommand(sqlStr, con)
'con.Open()
dr = cmd.ExecuteReader()
hasRows = dr.HasRows
While (hasRows And dr.Read)
strRelease = dr.Item(0).ToString
strItem2 = dr.Item(2).ToString
strItem3 = dr.Item(3).ToString
If (strItem2.Contains("27 PRODWK")) Then
dgvRow.Cells(1).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("28 SHIPMON")) Then
dgvRow.Cells(2).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("30 %COMPL")) Then
dgvRow.Cells(3).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("31 TGTSHIP")) Then
dgvRow.Cells(4).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("70 SCHPROD")) Then
dgvRow.Cells(5).Value = dr.Item(3).ToString.Trim
End If
If (strItem2.Contains("81 AB%")) Then
dgvRow.Cells(6).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("82 ARM%")) Then
dgvRow.Cells(7).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("83 SHAFT%")) Then
dgvRow.Cells(8).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("84 FIT% ")) Then
dgvRow.Cells(9).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("85 HDW%")) Then
dgvRow.Cells(10).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("86 FIN% ")) Then
dgvRow.Cells(11).Value = dr.Item(4).ToString.Trim
End If
If (strItem2.Contains("87 WELDCMP")) Then
dgvRow.Cells(12).Value = dr.Item(3).ToString.Trim
End If
End While
cmd.Dispose()
End If
End If
Next
End Using
Exit Sub
errH:
MsgBox(Err.Description)
con = Nothing
End Sub
-
Dec 13th, 2020, 01:18 AM
#11
Re: In Vb.net I need to add a message box to current code with Red backcolor
So, you need to run my code that OptionBase1 improved in post #7.
Run that code, and if found = false, do your updates, else show a msgbox and exit sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 13th, 2020, 02:31 AM
#12
Re: In Vb.net I need to add a message box to current code with Red backcolor
Your errorhandler...
That's legacy code. In .Net, we use a Try, Catch block...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 13th, 2020, 02:33 AM
#13
Re: In Vb.net I need to add a message box to current code with Red backcolor
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|