On a VB form why are graphics disappearing when the form is first displayed? - vb.net

I have a form which displays graphs of various kinds, taken from sales data from a database.
These graphs are drawn on panels on the form but I have had a very recent issue. Up until recently, the form displayed the graphs when it was displayed but now the graphs appear then disappear and I cannot figure out why.
Here is a link to a Vimeo video showing the behaviour: https://vimeo.com/575969787
When the form is loaded a procedure is called in the Form_Shown event which draws the graphs - for ease of understanding I have restricted this to one graph only.
Private Sub FormDashboard_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'Once form is initialised and shown run all the reports
RunReports(True)
End Sub
Private Sub RunReports(All As Boolean)
'Display these reports
DrawDepartment(PanelFood, TableDepartment)
'DrawDepartment(PanelWine, TableDepartment)
'DrawDivision(PanelDivision, TableDepartment)
'DrawCLA(PanelCLA, TableCLA)
''If all = true then also display MAT
'If (All = True) Then
' DrawMAT(PanelMAT, TableMAT)
'End If
End Sub
Public Shared Sub DrawDepartment(TargetPanel As Panel, TableGraph As DataTable)
'Graphics object
Dim Dep_Graph As Graphics = TargetPanel.CreateGraphics
Dim Offset As Integer = 45
Dim DepName As String = ""
'Graph area
With TargetPanel
GraphHeight = .Height - (Offset + Gap)
GraphWidth = .Width - (2 * Gap)
GraphXOrigin = Gap
GraphYOrigin = (Offset + Gap)
End With
Dim Filter As String = "" 'Filter used for querying the data table
Dim FoundMax() As DataRow 'A data row generated by a filter
Dim FoundRows() As DataRow 'A data row generated by a filter
'The data table has a max value for food sales in the given period
If (TargetPanel.Name.Contains("Food")) Then
Filter = "Department LIKE 'F%' OR Department LIKE 'S%'"
Else
Filter = "Department LIKE 'W%'"
End If
Select Case Report
Case ReportMode.Day
TableGraph.DefaultView.Sort = "Day DESC"
FoundMax = TableGraph.Select(Filter)
MaxDataValue = FoundMax(0).Item("Day")
Case ReportMode.Week
TableGraph.DefaultView.Sort = "Week DESC"
FoundMax = TableGraph.Select(Filter)
MaxDataValue = FoundMax(0).Item("Week")
Case ReportMode.Month
TableGraph.DefaultView.Sort = "Month DESC"
FoundMax = TableGraph.Select(Filter)
MaxDataValue = FoundMax(0).Item("Month")
Case ReportMode.Year
TableGraph.DefaultView.Sort = "Year DESC"
FoundMax = TableGraph.Select(Filter)
MaxDataValue = FoundMax(0).Item("Year")
End Select
TableGraph.DefaultView.Sort = "Department"
'Select only the food department values
If (TargetPanel.Name.Contains("Food")) Then
Filter = "Department LIKE 'F%' OR Department LIKE 'S%'"
Else
Filter = "Department LIKE 'W%'"
End If
FoundRows = TableGraph.Select(Filter)
'Calculate the width of each column
Dim NumberOfColumns As Integer = (FoundRows.GetUpperBound(0) + 1)
ColWidth = (GraphWidth - ((NumberOfColumns) * Gap)) / NumberOfColumns
'Using the graphics object draw the columns
With Dep_Graph
MessageBox.Show("Clearing graph area")
'Using the FoundMax data row extract the Day max value
ClearGraph(TargetPanel)
For Counter As Integer = 0 To FoundRows.GetUpperBound(0)
Select Case Report
Case ReportMode.Day
DataValue = FoundRows(Counter).Item("Day")
DepName = FoundRows(Counter).Item("Department")
If (TargetPanel.Name.Contains("Wine")) Then
ReportLabel(Counter + 12).Text = GetLabelData(DepName, DataValue)
Else
ReportLabel(Counter).Text = GetLabelData(DepName, DataValue)
End If
Case ReportMode.Week
DataValue = FoundRows(Counter).Item("Week")
DepName = FoundRows(Counter).Item("Department")
If (TargetPanel.Name.Contains("Wine")) Then
ReportLabel(Counter + 12).Text = GetLabelData(DepName, DataValue)
Else
ReportLabel(Counter).Text = GetLabelData(DepName, DataValue)
End If
Case ReportMode.Month
DataValue = FoundRows(Counter).Item("Month")
DepName = FoundRows(Counter).Item("Department")
If (TargetPanel.Name.Contains("Wine")) Then
ReportLabel(Counter + 12).Text = GetLabelData(DepName, DataValue)
Else
ReportLabel(Counter).Text = GetLabelData(DepName, DataValue)
End If
Case ReportMode.Year
DataValue = FoundRows(Counter).Item("Year")
DepName = FoundRows(Counter).Item("Department")
If (TargetPanel.Name.Contains("Wine")) Then
ReportLabel(Counter + 12).Text = GetLabelData(DepName, DataValue)
Else
ReportLabel(Counter).Text = GetLabelData(DepName, DataValue)
End If
End Select
If (MaxDataValue > 0) Then
'Set column height to a scale of the maximum value
ColHeight = (DataValue * GraphHeight) / MaxDataValue
End If
'Set colours
PenWWC.Color = Colors(Counter + 2)
BrushWWC = New SolidBrush(Colors(Counter + 2))
'Draw and fill
DrawColumn(Dep_Graph, GraphXOrigin, TargetPanel.Height - ColHeight - Offset, ColWidth, ColHeight, PenWWC, BrushWWC)
'Reset X0 to the next point
GraphXOrigin = GraphXOrigin + ColWidth + Gap
Next
End With
GraphXOrigin = Gap
Dep_Graph.Dispose()
End Sub
As can be seen in the video I have included a message box at the point where I clear the graphing area to show that this is NOT what is happening.
As I say, up until recently the form displayed with graphs showing; no changes have been made to this section of the application code so i don't know what is happening.
Further, as shown in the video, if I select the date for which the form is set, the graphs appear and if I change date, the new graphs appear, without problem.
Any suggestions most welcome. AFAIK I have posted all the relevant code here.
TIA,
Dermot

Related

Update previous row

I have an Excel file that contains some datas that I want to export into an Access db. In the C column I've got a field called 'Description'. Usually this field occupy just one cell, but it can happens that is more long.
In this case, for example, AP.01 has got 5 rows of description. How can update the first row with the next rows?
Public Sub updateDB(ByVal PathDB As String, str As String, id As Integer)
Dim db As New cDB
Dim v As New cVoce
Dim rs As ADODB.Recordset = db.RecordSet
v.Description = str
db.connetti_DB(PathDB)
db.get_rs("UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'")
End Sub
Public Function get_rs(ByVal query As String) As ADODB.Recordset
If db Is Nothing Then rs = Nothing : Return rs
rs = New ADODB.Recordset
rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
rs.LockType = ADODB.LockTypeEnum.adLockOptimistic
rs.Open(query, db)
Return rs
End Function
This code doesn't work because I update my current row, for this reason is useless the UPDATE instruction. How can I fix my code?
EDIT I post here the For loop
For r = 2 To grid.RowCount - 1
vett = Split(grid(r, 1).Text)
total = UBound(Split(grid(r, 1).Text, "."))
If grid(r, 1).Text <> "" Then
Select Case total
Case 0
Dim chapter As New cChapter
flag = 1
id = id + 1
chapter.Cod = grid(r, 1).Text.Substring(0, 1)
chapter.Description = grid(r, 3).Text
If Left(vett(0), 1) >= Chr(65) And Left(vett(0), 1) <= Chr(90) Then
chapter.Cod = Left(vett(0), 1)
oldChap = chap.Cod
If chapter.Cod <> oldCap Then
chapters.Add(chapter)
End If
End If
chapters.Add(chapter)
stringChap = chap.Description
Dim par As New cParagraph
If Left(vett(0), 2) >= Chr(65) And Left(vett(0), 2) <= Chr(90) Then
par.Cod = Left(vett(0), 2)
par.Cod_Chapter = Left(vett(0), 1)
oldPar = par.Cod
If par.Cod <> oldPar Then
paragraphs.Add(par)
End If
End If
If grid(r, 3).Text.Length > 255 Then
par.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
par.Description = grid(r, 3).Text.ToString
End If
paragraphs.Add(par)
stringPar = par.Description
Case 1
flag = 2
id = id + 1
c_Voc = voc.Cod_Chapter
p_Voc = voc.Cod_Paragraph
voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1)
voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0)
voc.Cod_Voice = Right(vett(0), 2)
If grid(r, 3).Text.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
voc.Description = grid(r, 3).Text.ToString
If voc.Description.EndsWith("-") Then
a = Replace(voc.Description, "-", "")
voc.Description = a
End If
End If
stringVoice = voc.Description
voices.Add(voc)
voices.Save_DB(dbDest)
Case 2
flag = 3
id = id + 1
sVoice = New cVoice
oldSvoice = voice.Cod_SVoice
sVoice.Cod_SVoice = Left(vett(0), 2)
If sVoice.Cod_SVoce <> oldSvoice Then
voices.Add(sVoice)
voices.Save_DB(dbDest)
End If
If grid(r, 3).Text.Length > 255 Then
sVoice.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
sVoice.Description = grid(r, 3).Text
End If
stringSvoice = sVoice.Description
sVoice.Cod_Voce = Left(vett(0), 5)
sVoice.Price1 = grid(r, 12).Text
sVoice.Price2 = sVoice.Price1
sVoice.UniMi = grid(r, 11).Text
sVoce.Sep = "."
voices.Add(sVoce)
voices.Save_DB(dbDest)
End Select
Else
If flag = 1 Then
stringChap = grid(r, 3).Text
chap.Description = stringChap & grid(r, 3).Text
stringPar = grid(r, 3).Text
paragraph.Description = stringPar & grid(r, 3).Text
End If
If flag = 2 Then
stringVoice = grid(r, 3).Text
voc.Description = voc.Description & stringVoice
voices.updateDB(dbDest, stringVoice, id)
voices.Add(voc)
End If
If flag = 3 Then
stringSvoice = grid(r, 3).Text
sVoice.Description = stringSvoice & grid(r, 3).Text
voices.Add(sVoice)
End If
chapter.Save_DB(dbDest)
paragraph.Save_DB(dbDest)
voice.Save_DB(dbDest)
End If
Next
EDIT2 I declared id As Integer and when Code column has a value then id=id+1. In this way I always know which row I have to modify. I modified also updateDB (now I'm using 3 parameters) and I added a WHERE condition into my query. Despite the update, nothing has changed
In database you cannot store records without PrimaryKey (actually you can, but it is bad idea). Since in your solution id is in fact Excel row number (sorry if I'm not correct but it looks like from code) it could be very hard to maintain it in future (in case someone add or remove description row). It would be better to change id column to text and use code as PK.
Storing description then could be solved in 2 ways:
1) Concatenate all rows containing description into 1 variable adding vbNewLine in between and store it in description field.
2) More relational but also more complex - create 2nd table for description with PK as i.e. autonumber, ForeignKey Code referring to main table. Maintenance will be here very complex. Not really worth effort.
Amount of changes in code is quite big, so sorry I'll not provide fixed code but I hope idea is clear.
BTW: The reason why description is not updated is described in your post. You are increasing id only when code is present, so every description field from first group have id = 1. The most simple fix in your code would be to create 2 update statements - One for rows with code
UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'
Second one for rows without code:
UPDATE Voice SET Description = Description + CHAR(13) + CHAR(10) + '" + v.Description + "' WHERE id= '"+id+"'

Dynamic checkbox events through commandbutton

I am currently programming a sheet which visualizes data sets in graphs. Because the user of this sheet will not need all the graphs, I would like to let them choose the ones needed through a UserForm. Since the amount of data sets is variable, the UserForm will have the same amount of checkboxes as there are datasets.
The Userform code is as follows.
Private Sub UserForm_Initialize()
Dim chkBoxA As MSForms.CheckBox
Dim chkBoxB As MSForms.CheckBox
Dim lblBox As MSForms.Label
Dim cnt As Control
Amount = Sheet4.Range("C4").Value 'Amount of datasets
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
CommandButton1.Left = 20
CommandButton1.Top = 40 + ((Amount - 1) * 40)
CommandButton1.TabIndex = Amount * 3 + 1
Me.Height = 220
Me.ScrollBars = fmScrollBarsVertical
Me.ScrollWidth = Me.InsideWidth * 9
For Each cnt In Me.Controls
If cnt.Top + cnt.Height > Me.ScrollHeight Then
Me.ScrollHeight = cnt.Top + cnt.Height + 5
End If
Next
End Sub
When the UserForm is filled in (graphs are chosen by clicking on the options), the user will press CommandButton1. An event should then be run to show the correct graph, but for the simplicity I am first testing if a MsgBox will show up. Unfortunately the MsgBox does not show up.
Private Sub CommandButton1_Click()
'Will fix this with a loop
If A1 = True Then
MsgBox ("TestA1")
End If
If B1 = True then
MsgBox ("TestB1")
End If
If A2 = True then
MsgBox ("TestA2")
End If
Unload Me
End Sub
I am stuck on this part. The checkboxes do show up on the UserForm and they are clickable, but the commandbutton only shuts down the sub (Unload Me). I would like to see the MsgBox show up when I select the corresponding option and click the commandbutton. Any help on getting this to work is appreciated!
You are referencing 'A1' in the sub, but that variable does not exitst at compile time, because you add them dynamically. What you need to do is loop the controls, to check the names. Best practice is to put the checkboxes in a frame, to be able to group them.
Add a frame to the userform and name it 'checkboxframe'
And then instead of:
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
you would need to do:
With Me.checkboxframe
For i = 1 To Amount
Set lblBox = .Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = .Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = .Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
End With
And to add the checkboxes to the frame, use something like:
For Each ctr In UserForm1.frame("checkboxframe").Controls
If TypeName(ctr) = "CheckBox" Then
If ctr.Value = True Then
'do something usefull here
msgbox ctr.name
End If
End If
Next ctr
The reason nothing appears is because there is no object "A1" manually defined as a variable.
To get the value of the box you Dynamically named "A1" you would have to refer to it as such:
If Me.Controls.Item("A1").Value = True then
Hope this helps!

Moving textbox output into a different textbox

I am trying to output text into another text box once the first has 5 entries in it. Example; i give the scores 100,200,300,200,200. Now when I try to enter a new score it should place it in the next textbox, but doesent.
Dim Testint As Integer ' define an Integer for testing
Dim sampleTextBox(3) As TextBox
sampleTextBox(0) = txtPlayer1Scores
sampleTextBox(1) = txtPlayer2Scores
sampleTextBox(2) = txtPlayer3Scores
sampleTextBox(3) = txtPlayer4Scores
Dim sampleLabel(3) As Label
sampleLabel(0) = lblPlayer1Average
sampleLabel(1) = lblPlayer2Average
sampleLabel(2) = lblPlayer3Average
sampleLabel(3) = lblPlayer4Average
scoreArray(textCount, gameNumber - 1) = CInt(txtScoreInput.Text) ' subtracting 1 from the score array
sampleTextBox(textCount).Text &= " Score:" & scoreArray(textCount, gameNumber - 1) & vbCrLf
'output statement
gameNumber = gameNumber + 1 'increment the counter
If gameNumber > MAX_SCORE_LENGTH Then
sampleTextBox(textCount).Focus()
sampleTextBox(textCount).Enabled = False
For i As Integer = 0 To 4 'Add the array values up
scoreTotal += scoreArray(textCount, i)
Next
playerAverage = scoreTotal / MAX_SCORE_LENGTH
sampleLabel(labelCount).Text = playerAverage
' I need the textbox switch here
textCount = textCount + 1
labelCount = labelCount + 1 ' and labels
ElseIf textCount > MAX_PLAYERS Then
'calculate team average
btnEnterScore.Enabled = False
Else
lblEnterScore.Text = "Enter Score for game #" & gameNumber ' 5 scores have not be inputted,
txtScoreInput.Text = "" 'ask for more
txtScoreInput.Focus() 'refocus the input textbox
End If
Fixed it...setting the max length to 15 forces it to move on after 15 digits, it also works when there are less than 15 digits
txtscore1.MaxLength = 15

datagrid view change color depend of status in winform application

I have a function like this:
Sub KPIColorchange()
'take 80 percenatge
Dim perc80 As Integer = 80
value80 = DefaultKPI * perc80 / 100
Dim carid As String
Dim strr As String = "select t.TBarcode as carid from Khanger_tbl k
inner join transaction_tbl t on k.transactid=t.transactID"
strr = strr + " where tid= " & tid & " and requested=1 and delivered=0 and status=3
and DATEDIFF(n, CAST(paydate AS DATETIME), GETDATE()) >=" & value80 & ""
Dim cmdr As New SqlCommand(strr, con.connect)
dr2 = cmdr.ExecuteReader
While dr2.Read
If dr2("carid") Is DBNull.Value Then
carid = ""
Else
carid = dr2("carid")
End If
Dim cnt As Integer = DGVall.Rows.Count
For i = 0 To cnt - 2
If DGVall.Rows(i).Cells(0).Value.ToString().Equals(carid) Then
DGVall.Rows(i).DefaultCellStyle.BackColor = Color.Fuchsia
End If
Next
End While
dr2.Close()
con.disconnect()
'take 100 percentage
Dim perc100 As Integer = 100
value100 = DefaultKPI * perc100 / 100
Dim str100 As String = "select t.TBarcode as carid from Khanger_tbl k
inner join transaction_tbl t on k.transactid=t.transactID"
str100 = str100 + " where tid= " & tid & " and requested=1 and delivered=0 and status=3 and
DATEDIFF(n, CAST(paydate AS DATETIME), GETDATE()) >=" & value100 & ""
Dim cmd100 As New SqlCommand(str100, con.connect)
dr2 = cmd100.ExecuteReader
While dr2.Read
If dr2("carid") Is DBNull.Value Then
carid = ""
Else
carid = dr2("carid")
End If
Dim cnt As Integer = DGVall.Rows.Count
For i = 0 To cnt - 2
If DGVall.Rows(i).Cells(0).Value.ToString().Equals(carid) Then
DGVall.Rows(i).DefaultCellStyle.BackColor = Color.OrangeRed
End If
Next
End While
dr2.Close()
con.disconnect()
End Sub
I have a grid view ,in that grid view i am loading some Requested carid we already set some time interval to Location.depend on the location time interval i have to change color in grid view
if the car requested time coming 80 percentage of location interval time then i will change to one color .
if the car requested time coming 100 percentage of location interval time then i will change to OrangeRed only this much things i am doing here..normally my datagrid view contains morethan 100 above records. this function i am calling in Timer_tick event in each one minute.
but becouse of this function my system get slow or some time system get hang.how i can optimize this code?
any help is very appreciable in advance

Implementing a Variable in a Label Name in VB?

I currently have a huge IF statement and want to minimize it as much as possible.
I have a datagrid that i am populating from a SQL query and then from this datagrid i am passing the values into seperate labels.
I am able to create a For Each Loop in which i cycle through the variables looking until a counter reaches 7. However the problem arises when i need to incremente the Label name values by one. Each time, so essentially i need to add a counter variable into the Label name.
The code that i need to minimize is:
result73 = DataGridView1.Rows(0).Cells(0).Value.ToString
result74 = DataGridView1.Rows(0).Cells(1).Value.ToString
result75 = DataGridView1.Rows(1).Cells(0).Value.ToString
result76 = DataGridView1.Rows(1).Cells(1).Value.ToString
result77 = DataGridView1.Rows(2).Cells(0).Value.ToString
result78 = DataGridView1.Rows(2).Cells(1).Value.ToString
result79 = DataGridView1.Rows(3).Cells(0).Value.ToString
result80 = DataGridView1.Rows(3).Cells(1).Value.ToString
result81 = DataGridView1.Rows(4).Cells(0).Value.ToString
result82 = DataGridView1.Rows(4).Cells(1).Value.ToString
result83 = DataGridView1.Rows(5).Cells(0).Value.ToString
result84 = DataGridView1.Rows(5).Cells(1).Value.ToString
result85 = DataGridView1.Rows(6).Cells(0).Value.ToString
result86 = DataGridView1.Rows(6).Cells(1).Value.ToString
result87 = DataGridView1.Rows(7).Cells(0).Value.ToString
result88 = DataGridView1.Rows(7).Cells(1).Value.ToString
If result73 = "Monday" Then
DaySalesLbl1.Text = result74
ElseIf result73 = "Tuesday" Then
DaySalesLbl2.Text = result74
ElseIf result73 = "Wednesday" Then
DaySalesLbl3.Text = result74
ElseIf result73 = "Thursday" Then
DaySalesLbl4.Text = result74
ElseIf result73 = "Friday" Then
DaySalesLbl5.Text = result74
ElseIf result73 = "Saturday" Then
DaySalesLbl6.Text = result74
ElseIf result73 = "Sunday" Then
DaySalesLbl7.Text = result74
End If
This If Statement goes on for each variable that is declared above.
The Loop I have created looks something like this:
Dim cou As Integer
Dim n As Integer
cou = 0
n = 1
Do Until result74 <> ""
If result73 = cou Then
DaySalesLbl +n = result74
End If
cou = cou + 1
n = n + 1
Loop
But the section DaySalesLbl +n = result74 brings an Error because it looks for a generated method.
If, as I suspect, this is a WinForm project, then you can access the controls by name using the Form.Controls collection, like this:
Dim cou As Integer
Dim n As Integer
cou = 0
n = 1
Do Until result74 <> ""
If result73 = cou Then
Dim l As Label = CType(Me.Controls("DaySalesLbl" & n), Label)
l.Text = result74
End If
cou = cou + 1
n = n + 1
Loop
The Controls collection contains all the controls that are direct children of the form. You can access them by index (e.g. Me.Controls(0)), or by name (e.g. Me.Controls("DaySalesLbl6")). However, the collection stores the list as the base Control type, so you have to cast it to a the specific type before accessing the specific properties. That is why the CType(..., Label) is there in the example.
I would write code as follows:
For i as Integer = 0 to 7
Select Case DataGridView1.Rows(i).Cells(1).Value.ToString
Case "Monday"
Me.Controls("DaySalesLbl" & i+1).Text = DataGridView1.Rows(i).Cells(2).Value.ToString
Case "Tuesday"
Me.Controls("DaySalesLbl" & i+2).Text = DataGridView1.Rows(i).Cells(2).Value.ToString
... etc ...
I did this without the IDE, so I hope I didn't mess anything up, but I hope this sets you in the right direction and helps.