Programming dynamic panels in VB.NET - vb.net

My programming experience is short and I have written several applications but am very much still a novice in VB.NET.
I am writing an application which requires information to be displayed in panels. These panels need to be created at runtime based upon the information from a database. The database contains the information and I will create the panels and put the data into it. The data will be dynamic labels which will update with information to the user.
My problem is that I've never really created dynamic controls before and I seem to be having problems. If I select one item in the database to be created then it works fine. More than one is where I'm having the issue. It goes through the code and adds the controls to the form but only actually displays the last panel it created. The space on the form where the other panels should be is blank.
Also, I want to refer to the dynamically created panels after they are created. I am giving them each a unique name as they are created. Obviously I can't refer to their name directly in the IDE, Is there another way of referencing the name I am giving them?
My code is below, if anyone can point me in the right direction as to what I'm missing and doing wrong it would be much appreciated.
Dim cmd03 As OleDb.OleDbCommand = New OleDb.OleDbCommand(Nothing, strConnection)
Dim strMachineID As String = String.Empty
Dim strMachineIP As String = String.Empty
Dim strMTB As String = String.Empty
Dim MachinePanel As New Panel
Dim MachineName As New Label
Dim MachineStatus As New Label
Dim RunningProg As New Label
Dim RunningPart As New Label
Dim PartsComplete As New Label
Dim startpointX As Integer = 12
Dim startpointY As Integer = 82
Try
cmd03.CommandText = "Select * FROM tblMachines WHERE tblMachines.Monitor = TRUE"
strConnection.Open()
Dim myreader02 As OleDb.OleDbDataReader = cmd03.ExecuteReader(CommandBehavior.CloseConnection)
While myreader02.Read
strMachineID = myreader02.GetValue(0)
strMachineIP = myreader02.GetValue(10)
strMTB = myreader02.GetValue(1)
MachinePanel.Name = "pnl" & strMTB & strMachineID
MachinePanel.Size = New Point(918, 120)
MachinePanel.Location = New Point(startpointX, startpointY)
MachinePanel.BackColor = Color.White
'MachinePanel.Visible = True
'MachinePanel.Enabled = True
MachineName.Name = "lbl" & strMTB & strMachineID
MachineName.Text = strMTB & Chr(32) & strMachineID
MachineName.Font = New Font("Bookman Old Style", 26, FontStyle.Bold)
MachineName.Location = New Point(4, 34)
MachineName.AutoSize = True
MachineStatus.Name = "lbl" & strMTB & strMachineID & "status"
MachineStatus.Text = "?Unknown?"
MachineStatus.Font = New Font("Bookman Old Style", 22.2, FontStyle.Bold)
MachineStatus.Location = New Point(380, 34)
MachineStatus.AutoSize = True
MachineStatus.Enabled = True
MachineStatus.Visible = True
RunningProg.Name = "lbl" & strMTB & strMachineID & "prog"
RunningProg.Text = "prog????"
RunningProg.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
RunningProg.Location = New Point(760, 9)
RunningProg.AutoSize = True
RunningPart.Name = "lbl" & strMTB & strMachineID & "part"
RunningPart.Text = "part????"
RunningPart.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
RunningPart.Location = New Point(760, 44)
RunningPart.AutoSize = True
PartsComplete.Name = "lbl" & strMTB & strMachineID & "complete"
PartsComplete.Text = "complete????"
PartsComplete.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
PartsComplete.Parent = MachinePanel
PartsComplete.Location = New Point(760, 78)
PartsComplete.AutoSize = True
' PartsComplete.Enabled = True
'PartsComplete.Visible = True
Me.Controls.Add(MachinePanel)
MachinePanel.Controls.Add(MachineName)
MachinePanel.Controls.Add(MachineStatus)
MachinePanel.Controls.Add(RunningProg)
MachinePanel.Controls.Add(RunningPart)
MachinePanel.Controls.Add(PartsComplete)
MachinePanel.Visible = True
MachinePanel.Enabled = True
MachinePanel.Show()
startpointY = startpointY + 140
End While
Catch ex As Exception
Finally
strConnection.Close()
End Try
End Sub

First, turn option strict on as MachinePanel.Size = New Point(918, 120) accepts a Size structure and not a Point structure. Secondly you'll need to create a new instance of each object in each cycle.
While myreader02.Read
MachinePanel = New Panel() '
MachineName = New Label()
MachineStatus = New Label()
RunningProg = New Label()
RunningPart = New Label()
PartsComplete = New Label()
With MachinePanel
.Name = "pnl" & strMTB & strMachineID
.Size = New Size(918, 150)
.Location = New Point(startpointX, startpointY)
.BackColor = Color.White
End With
With MachineName
.Name = "lbl" & strMTB & strMachineID
.Text = strMTB & Chr(32) & strMachineID
.Font = New Font("Bookman Old Style", 26, FontStyle.Bold)
.Location = New Point(0, 0) '<- In relation to MachinePanel.ClientRectangle
.AutoSize = True
End With
With MachineStatus
.Name = "lbl" & strMTB & strMachineID & "status"
.Text = "?Unknown?"
.Font = New Font("Bookman Old Style", 22.2, FontStyle.Bold)
.Location = New Point(0, 30)
.AutoSize = True
.Enabled = True
.Visible = True
End With
With RunningProg
.Name = "lbl" & strMTB & strMachineID & "prog"
.Text = "prog????"
.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
.Location = New Point(0, 60)
.AutoSize = True
End With
With RunningPart
.Name = "lbl" & strMTB & strMachineID & "part"
.Text = "part????"
.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
.Location = New Point(0, 90)
.AutoSize = True
End With
With PartsComplete
.Name = "lbl" & strMTB & strMachineID & "complete"
.Text = "complete????"
.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
.Parent = MachinePanel
.Location = New Point(0, 120)
.AutoSize = True
End With
MachinePanel.Controls.AddRange(New Control() {MachineName, MachineStatus, RunningProg, RunningPart, PartsComplete})
Me.Controls.Add(MachinePanel)
startpointY = startpointY + 150
End While

Related

How to auto display form Update in vb.net?

A part of a system I am creating has a kitchen display system but I dont get how to automatically display the contents in my form
Public Sub GetOrders()
Try
kitchenFlowLayoutPanel.Controls.Clear()
Dim i As Integer
Dim sql As String
sql = "SELECT c.cart_id, c.invoice_no, c.table_no, c.sale_time, f.item_name, c.serve_size, c.quantity FROM tbl_cart AS c INNER JOIN tbl_food AS f on f.item_id = c.items_id WHERE c.status='Pending'"
conn.Open()
cmd = New MySqlCommand(sql, conn)
cmd.ExecuteReader()
conn.Close()
Dim dTable1 As New DataTable()
Dim dAdaptor1 As New MySqlDataAdapter(cmd)
dAdaptor1.Fill(dTable1)
Dim panel1 As FlowLayoutPanel
Dim panel2 As FlowLayoutPanel
For i = 0 To dTable1.Rows.Count - 1
panel1 = New FlowLayoutPanel
panel1.AutoSize = True
panel1.Width = 260
panel1.Height = 500
panel1.FlowDirection = FlowDirection.TopDown
panel1.BorderStyle = BorderStyle.FixedSingle
panel1.Margin = New Padding(10, 10, 10, 10)
panel2 = New FlowLayoutPanel
panel2.BackColor = Color.FromArgb(50, 55, 89)
panel2.AutoSize = True
panel2.Width = 230
panel2.Height = 125
panel2.FlowDirection = FlowDirection.TopDown
panel2.Margin = New Padding(0, 0, 0, 0)
Dim label1 As New Label
label1.ForeColor = Color.White
label1.Margin = New Padding(10, 5, 3, 0)
label1.Font = New Font(label1.Font, FontStyle.Bold)
label1.AutoSize = True
Dim label2 As New Label
label2.ForeColor = Color.White
label2.Margin = New Padding(10, 5, 3, 0)
label2.AutoSize = True
Dim label3 As New Label
label3.ForeColor = Color.White
label3.Margin = New Padding(10, 5, 3, 0)
label3.AutoSize = True
Dim label4 As New Label
label4.ForeColor = Color.White
label4.Margin = New Padding(10, 5, 3, 0)
label4.AutoSize = True
Dim label5 As New Label
label5.ForeColor = Color.White
label5.Margin = New Padding(10, 5, 3, 0)
label5.AutoSize = True
label1.Text = "Entry ID : " & dTable1.Rows(i)("cart_id").ToString
label2.Text = "Invoice No : " & dTable1.Rows(i)("invoice_no").ToString
label3.Text = "Table No : " & dTable1.Rows(i)("table_no").ToString
label4.Text = "Order Time : " & dTable1.Rows(i)("sale_time").ToString
label5.Text = Environment.NewLine & "ORDERED ITEM : " & Environment.NewLine & Environment.NewLine & " Item : " & dTable1.Rows(i)("item_name").ToString & Environment.NewLine & " Serve Size : " & dTable1.Rows(i)("serve_size").ToString & Environment.NewLine & " Qty: " & dTable1.Rows(i)("quantity").ToString & Environment.NewLine & Environment.NewLine
panel2.Controls.Add(label1)
panel2.Controls.Add(label2)
panel2.Controls.Add(label3)
panel2.Controls.Add(label4)
panel2.Controls.Add(label5)
panel1.Controls.Add(panel2)
Dim btn As New Guna.UI2.WinForms.Guna2Button
btn.AutoRoundedCorners = True
btn.Size = New Size(100, 35)
btn.FillColor = Color.FromArgb(241, 85, 126)
btn.Margin = New Padding(60, 5, 3, 10)
btn.Location = New Point(0, 0)
btn.Text = "Complete"
btn.Tag = dTable1.Rows(i)("cart_id").ToString ' store id
AddHandler btn.Click, AddressOf btn_Click
panel2.Controls.Add(btn)
kitchenFlowLayoutPanel.Controls.Add(panel1)
Next
Catch ex As Exception
conn.Close()
MsgBox(ex.Message, vbCritical)
End Try
End Sub
I tried putting it in a do loop but it shows errors even before I can log in to the system.
I have also tried to use incorporate "GetOrders().refresh" but it was also not working.
It is difficult to understand what exactly you're trying to achieve.
In this example, a button named RefreshButton was added to the Form.
This shows your GetOrders method getting called from the ButtonClick event.
Private Sub GetOrders()
'your GetOrders code...
End Sub
Private Sub RefreshButton_Click(sender As Object, e As EventArgs) Handles RefreshButton.Click
Try
GetOrders() ' this will call your GetOrders method
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub

Access added (sub) control in VB .NET

With this code I dynamicly add a panel with two panels inside (a header and a data panel). Within the Data panel there is also a label that I like to access.
Now I like to access the label inside the data panel but can't reach it with:
test_label.text = "this a second test"
Here the dynamicaly added panels
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newPanelMain As Panel = New Panel With {
.Location = New Point(200, 500),
.Name = "test",
.Size = New Size(500, 500)
}
Dim newPanelHeader As Panel = New Panel With {
.Name = newPanelMain.Name & "_header",
.BackColor = Color.Orange,
.Dock = DockStyle.Top,
.Height = 50
}
newPanelMain.Controls.Add(newPanelHeader)
Dim newPanelData As Panel = New Panel With {
.Name = newPanelMain.Name & "_data",
.Dock = DockStyle.Fill,
.BorderStyle = BorderStyle.FixedSingle
}
newPanelMain.Controls.Add(newPanelData)
Dim newPanelSize As Panel = New Panel With {
.Name = newPanelMain.Name & (("_size")),
.BackColor = Color.Red,
.Height = 20,
.Width = 20,
.Location = New Point(newPanelData.Width - 20, newPanelData.Height - 20)
}
newPanelData.Controls.Add(newPanelSize)
Dim newLabel As Label = New Label With {
.Text = "This is a test",
.Name = newPanelMain.Name & (("_label")),
.Location = New Point(0, 0),
.AutoSize = True
}
newPanelData.Controls.Add(newLabel)
Me.Controls.Add(newPanelMain)
End Sub
Declare newLabel as a member variable:
Private newLabel As Label
So you can use this code:
Me.newLabel = New Label With {
.Text = "This is a test",
.Name = newPanelMain.Name & (("_label")),
.Location = New Point(0, newPanelHeader.Height + 10),
.AutoSize = True
}
newPanelData.Controls.Add(Me.newLabel)
And then change its .Text property using:
Me.newLabel.Text = "this a second test"
In a different situation (e.g. if you have multiple Labels created at runtime) remember that you can use Control.ControlCollection class:
CType(CType(Me.Controls("test"), Panel).Controls("test_data"), Panel).Controls("test_label").Text = "this is a third test"

Getting the famous "NullReferenceException" error while building form with dynamic groupboxes in VB.NET

Of course I've searched through all types of forums relating to the "NullReferenceException" error and they all say that my objects = nothing and that I need to use the "new' operand to make it work.
I'm building my Form dynamically using groupboxes and then inserting labels and textboxes in each groupbox. Some textboxes will need to be regularly updated but I don't want to update the whole form unnecessarily since it makes the Form flicker when updating(I've tried).
I've was able to build my form normally using the "new groupbox" function but was unable to access objects in each of the groupboxes once it was built. Maybe it has something to do with the fact that I didn't make an indexed array out of the groupboxes. Rebuilding the whole Form was my only way to refresh he displayed values, but like I stated, it makes the the Form flicker.
So I tried to make an indexed array out of the Groupboxes, this is where I get the "NullRefereceException" error. I've already declare my Groupbox array.
Public Shared GrpBox as GroupBox()
Here is my code at this point:
For j As Integer = 0 To Thermostats.Count - 1
Dim ThermostatName As String
Dim Model As String
Dim IPAddress As String
With Thermostats(j)
ThermostatName = .ThermostatName
Model = .Model
IPAddress = .IPAddress
End With
GrpBox(j) = New GroupBox() With {
.Location = New Point(265, 27),
.Size = New Size(253, 176),
.Text = ThermostatName,
.BackColor = Color.White,
.FlatStyle = BorderStyle.Fixed3D,
.BackgroundImage = Imagelist1.Images(0),
.Margin = New Padding(20, 30, 0, -10)
}
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(146, 65),
.AutoSize = True,
.Text = "Heat To",
.Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
.Visible = True
})
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(210, 80),
.AutoSize = True,
.Text = TempUnit,
.Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
.Visible = True
})
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(146, 110),
.AutoSize = True,
.Text = "Cool To",
.Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
.Visible = True
})
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(210, 125),
.AutoSize = True,
.Text = TempUnit,
.Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
.Visible = True
})
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(15, 66),
.Size = New Size(125, 15),
.Text = "Current Temperature",
.Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
.Visible = True
})
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(90, 80),
.AutoSize = True,
.Text = TempUnit,
.Font = New Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold),
.Visible = True
})
GrpBox(j).Controls.Add(New Label() With {
.Location = New Point(20, 20),
.AutoSize = True,
.Text = Model,
.Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
.Visible = True
})
Dim TxtCool As New TextBox() With {
.Location = New Point(150, 125),
.Size = New Drawing.Size(60, 25),
.Visible = True,
.Enabled = False,
.BackColor = Color.White,
.ForeColor = Color.Black,
.BorderStyle = BorderStyle.FixedSingle,
.Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
.Text = 23.0 '
}
GrpBox(j).Controls.Add(TxtCool)
Dim TxtHeat As New TextBox() With {
.Location = New Point(150, 80),
.Size = New Size(60, 25),
.Visible = True,
.Enabled = False,
.BackColor = Color.White,
.ForeColor = Color.Black,
.BorderStyle = BorderStyle.FixedSingle,
.Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
.Text = 20.5
}
GrpBox(j).Controls.Add(TxtHeat)
Dim TxtActualTemp As New TextBox With {
.Location = New Point(19, 82),
.Size = New Drawing.Size(75, 45),
.Visible = True,
.Enabled = False,
.BackColor = Color.White,
.ForeColor = Color.Black,
.BorderStyle = BorderStyle.FixedSingle,
.Font = New Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold),
.Text = (6 * Rnd() + 19)
}
GrpBox(j).Controls.Add(TxtActualTemp)
Me.TblLytPnl.Controls.Add(GrpBox(j))
Next

VB Net What is the fastest way to generate object created programmatically

I have create an application that need to generate about 100 to 1000 object programmatically. This object will be generated with the same amount in the database. In order to accomplish objective above, of course i need to call the data from database, then i looping through each data to generate each object. But it takes such a very long time before it done. I hoping that you have an alternative way beside how i do this.
Below is my code to call the data from database
Dim tbls As New DataTable
Using cons As New SqlConnection(stringconnection)
Dim sqls = "select * from tbsectiondetail where IDSection = '" & txtIDSection.Text &
"' AND KodeLocation like '%" & txtCari.Text &
"%' AND Status like '%" & cbStatus.Text &
"%' ORDER BY Status DESC"
Dim adps As New SqlDataAdapter(sqls, cons)
adps.Fill(tbls)
adps.Dispose()
cons.Dispose()
End Using
If i just generate the data only, its just take only 1 to 2 second. So i don't think there is problem about how i call the data from database
And below is how i generate the object through looping the data
Dim resultsx As DataRow() = tbls.Select("")
If resultsx.Count > 0 Then
For iX = 0 To resultsx.Count - 1
'CREATE A VARIABLE
Dim IDSectionDetail = resultsx(iX)("IDSectionDetail").ToString
Dim NamaLocation = resultsx(iX)("NamaLocation").ToString
Dim SectionKode = resultsx(iX)("KodeLocation").ToString
Dim StatusSection = resultsx(iX)("Status").ToString
Dim StatusSectionAt = resultsx(iX)("StatusAt").ToString
Dim StatusC1 = resultsx(iX)("StatusC1").ToString
Dim StatusC2 = resultsx(iX)("StatusC2").ToString
'GENERATE A PANEL
Dim pnlContainer As New Panel
pnlContainer.Size = New Size(290, 200)
Tablex.Controls.Add(pnlContainer, curColumn, rowNo)
pnlContainer.Dock = DockStyle.Fill
Dim pnlHeader As New Panel
pnlContainer.Controls.Add(pnlHeader)
pnlHeader.Dock = DockStyle.Top
pnlHeader.Size = New Size(pnlContainer.Width, 35)
If StatusSection = "Open" Then
pnlHeader.BackColor = Color.DarkSlateGray
Else
pnlHeader.BackColor = Color.FromArgb(185, 58, 50)
End If
pnlContainer.Controls.SetChildIndex(pnlHeader, 2)
'GENERATE LABEL HEADER
Dim lblHeader As New Label
lblHeader.Text = NamaLocation & " - " & SectionKode
pnlHeader.Controls.Add(lblHeader)
lblHeader.Font = New Font("Century Gothic", 9, FontStyle.Bold Or FontStyle.Italic)
lblHeader.ForeColor = Color.White
lblHeader.Location = New Point(8, 7)
lblHeader.AutoSize = True
'GENERATE LABEL STATUS
Dim lblStatus As New Label
lblStatus.Text = "Status : " & StatusSection
pnlHeader.Controls.Add(lblStatus)
lblStatus.Font = New Font("Century Gothic", 8, FontStyle.Bold Or FontStyle.Italic)
lblStatus.ForeColor = Color.White
lblStatus.TextAlign = ContentAlignment.MiddleRight
lblStatus.Height = pnlHeader.Height
lblStatus.Dock = DockStyle.Right
lblStatus.AutoSize = True
'GENERATE PANEL FOOTER
Dim pnlFooter As New Panel
pnlContainer.Controls.Add(pnlFooter)
pnlFooter.Dock = DockStyle.Fill
pnlFooter.Size = New Size(pnlFooter.Width, 35)
pnlFooter.BackColor = Color.White
pnlContainer.Controls.SetChildIndex(pnlFooter, 0)
'GENERATE BUTTON DETAIL
Dim btnDetail As New Button
pnlFooter.Controls.Add(btnDetail)
btnDetail.Name = "btn" & SectionKode
btnDetail.FlatStyle = FlatStyle.Flat
btnDetail.FlatAppearance.BorderColor = Color.FromArgb(46, 74, 98)
btnDetail.FlatAppearance.BorderSize = 1
btnDetail.FlatAppearance.MouseDownBackColor = Color.Gold
btnDetail.FlatAppearance.MouseOverBackColor = Color.Gold
btnDetail.BackColor = Color.DarkSlateGray
btnDetail.Text = "Detail"
btnDetail.Image = My.Resources.search2_icon
btnDetail.Font = New Font("Century Gothic", 8, FontStyle.Bold)
btnDetail.TextImageRelation = TextImageRelation.TextBeforeImage
btnDetail.Location = New Point(181, 84)
btnDetail.Anchor = AnchorStyles.Right And AnchorStyles.Top
btnDetail.Size = New Size(75, 28)
btnDetail.ForeColor = Color.White
AddHandler btnDetail.Click, Sub() DetailSection(IDSectionDetail, SectionKode)
curColumn += 1
If curColumn >= 5 Then
curColumn = 0
RowCount += 1
Tablex.RowCount = RowCount
rowNo += 1
End If
Next
End If
pnlFill.Controls.Add(Tablex)
Actually there is still more object to generate through that looping. But from my analysis, the looping already take such a long time already just from code above.
The result of the generated object
I just wonder if there is another and faster alternative to generate such object. Or do i need to give up to design and just make it simple black and white ?

Looping Datagridview Controls Using Array Wtih Event Handlers

I am creating a Sub Routine that would create datagridviews depending on my desired number of loops. I am assuming that the problem is that the columns that are to be created for each datagridview are all consolidated in the first created datagridview. The reason why I'm assuming this is because I'm seeing the other datagridviews without columns and rows (just the gray background). My assumption is that my column variable declarations are not appropriate for what I want to do. Please see my code.
Sub routine for creating Datagridview code:
Private Sub DrawGridViewWLoop()
Dim dgvIterator As Short
ReDim dgvControlArr(10)
For dvgIterator = 0 To 3
dgvControlArr(dvgIterator) = New System.Windows.Forms.DataGridView
With dgvControlArr(dgvIterator)
.Size = New System.Drawing.Size(0, 0)
.AutoSize = True
.EnableHeadersVisualStyles = False
.RowHeadersVisible = False
.BackgroundColor = Color.White
.BorderStyle = BorderStyle.None
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToOrderColumns = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.ReadOnly = True
.MultiSelect = False
End With
Dim colID_2 As New System.Windows.Forms.DataGridViewTextBoxColumn
With colID_2
.HeaderText = "ID"
.Name = "colID"
.HeaderCell.Style.BackColor = Color.SpringGreen
.HeaderCell.Style.ForeColor = Color.White
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Width = 300
End With
Dim colG_2 As New System.Windows.Forms.DataGridViewButtonColumn
With colG_2
.HeaderText = "G"
.Name = "colG"
.HeaderCell.Style.BackColor = Color.SpringGreen
.HeaderCell.Style.ForeColor = Color.White
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Width = 40
End With
Dim colI_2 As New System.Windows.Forms.DataGridViewTextBoxColumn
With colI_2
.HeaderText = "I"
.Name = "colI"
.HeaderCell.Style.BackColor = Color.SpringGreen
.HeaderCell.Style.ForeColor = Color.White
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
Dim colO_2 As New System.Windows.Forms.DataGridViewTextBoxColumn
With colO_2
.HeaderText = "O"
.Name = "colO"
.HeaderCell.Style.BackColor = Color.SpringGreen
.HeaderCell.Style.ForeColor = Color.White
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
Dim colQ_2 As New System.Windows.Forms.DataGridViewTextBoxColumn
With colQ_2
.HeaderText = "Q"
.Name = "colQ"
.HeaderCell.Style.BackColor = Color.SpringGreen
.HeaderCell.Style.ForeColor = Color.White
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
Dim colIn_2 As New System.Windows.Forms.DataGridViewTextBoxColumn
With colIn_2
.HeaderText = "In"
.Name = "colIn"
.HeaderCell.Style.BackColor = Color.SpringGreen
.HeaderCell.Style.ForeColor = Color.White
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
dgvControlArr(dgvIterator).Columns.AddRange(colID_2, colG_2, colI_2, colO_2, colQ_2, colIn_2)
With dgvControlArr(dgvIterator)
.Columns("colID").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("colG").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("colI").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("colO").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("colQ").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("colID").SortMode = DataGridViewColumnSortMode.NotSortable
.Columns("colG").SortMode = DataGridViewColumnSortMode.NotSortable
.Columns("colI").SortMode = DataGridViewColumnSortMode.NotSortable
.Columns("colO").SortMode = DataGridViewColumnSortMode.NotSortable
.Columns("colQ").SortMode = DataGridViewColumnSortMode.NotSortable
End With
With dgvControlArr(dgvIterator).Rows
.Add("01")
.Add("02")
.Add("03")
.Add("04")
.Add("05")
End With
dgvControlArr(dgvIterator).Rows(1).Cells(2).Value = "Y"
dgvControlArr(dgvIterator).Rows(0).Cells(5).Value = "sample 1"
dgvControlArr(dgvIterator).Rows(2).Cells(5).Value = "sample 3"
dgvControlArr(dgvIterator).Rows(3).Cells(5).Value = "sample 4"
dgvControlArr(dgvIterator).Rows(3).Cells(2).Value = "Y"
dgvControlArr(dgvIterator).Rows(0).Cells(2).Value = "Y"
AddHandler dgvControlArr(dgvIterator).SelectionChanged, AddressOf Me.DataGridViews_SelectionChanged
AddHandler dgvControlArr(dgvIterator).CellContentClick, AddressOf Me.DataGridViews_CellContentClick
Next
Me.Controls.AddRange(dgvControlArr)
GridViewStackOrder()
End Sub
Code For Ordering my datagridview locations
Private Sub GridViewStackOrder()
dgvControlArr(0).Location = New System.Drawing.Point(10, 250)
dgvControlArr(1).Location = New System.Drawing.Point(10, 750)
dgvControlArr(2).Location = New System.Drawing.Point(10, 1250)
dgvControlArr(3).Location = New System.Drawing.Point(10, 1750)
End Sub
Code For My Event Handlers
Private Sub DataGridViews_SelectionChanged(sender As System.Windows.Forms.DataGridView, e As EventArgs)
sender.ClearSelection()
End Sub
Private Sub DataGridViews_CellContentClick(sender As System.Windows.Forms.DataGridView, e As DataGridViewCellEventArgs)
If e.RowIndex < 0 OrElse Not e.ColumnIndex = sender.Columns(1).Index Then Return
If e.RowIndex >= 0 Then
If sender.Rows(e.RowIndex).Cells(2).Value = "Y" Then
Dim chosenID As String = sender.Rows(e.RowIndex).Cells(5).Value
MsgBox(chosenID)
Else
MsgBox("Input not available.")
End If
End If
End Sub
My objective here is to create columns and rows for all the gridview not just the first one. Also I would like to prohibit selection for all the datagridviews that's why I created the selection_changed handler. I know that this is pretty basic but please, your ideas will be very helpful. Thanks geniuses!
You have a typo in your code. You're declaring the counter as following:
Dim dgvIterator As Short
Now here is your problem, you're mixing dgv and dvg.
For dvgIterator = 0 To 3
dgvControlArr(dvgIterator) = New System.Windows.Forms.DataGridView
To fix this, just replace the above lines with the following lines:
For dgvIterator = 0 To 3
dgvControlArr(dgvIterator) = New System.Windows.Forms.DataGridView