how to add items in combox dynamically in tablelayoutpanel in vb.net - vb.net

i have comboboxes dynamically created in a tablelayoutpanel and i want to add items to that combobox dynamically( i dont want to use data sources).
here is my code
Dim cmb(maxx)
For a = 0 To maxx - 1
TableLayoutPanel1.Height += 31
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, 30))
TableLayoutPanel1.RowCount += 1
TableLayoutPanel1.Controls.Add(New CheckBox With {.Text = CopyaillongoDataSet.inventory.Rows(a).Item(1).ToString, .Font = New System.Drawing.Font("Microsoft Sans Serif", 10)}, 0, TableLayoutPanel1.RowCount - 1)
TableLayoutPanel1.Controls.Add(New ComboBox With {.Name = cmb(a), .MaxDropDownItems = 10, .DropDownStyle = ComboBoxStyle.DropDownList}, 1, TableLayoutPanel1.RowCount - 1)
cmb(a).items.add(a)
Next
i assigned a name to a combobox and tried to use that name to add items but were unsucessful. is it possible to create items dynamically with a combobox dynamically created? if so, can you help me? i am working on my thesis and i need to get rid of this problem. thank you for those who can help.

You can't reference a control by just a string name the way you are doing it in your code (you didn't post what you are doing between Dim cmd(maxx) and the loop, but I'm guessing you want to do something like this, where you create a new ComboBox control and add it to the Panel:
Dim newCombo As New ComboBox With {.MaxDropDownItems = 10,
.DropDownStyle = ComboBoxStyle.DropDownList}
newCombo.Items.Add(a)
TableLayoutPanel1.Controls.Add(newCombo, 1, TableLayoutPanel1.RowCount - 1)
You can reference the control later with TableLayoutPanel1.GetControlFromPosition(column, row) function.

Related

Autosize Rows for Datagridview in VB.net not working

Hi, i'm making a memo type of data grid with Dynamic data from Database
and somehow i cannot make the rows auto resize.
i tried pretty much everything, here's some snippet of my code which i put on Form Load sub:
DataGridView2.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView2.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView2.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView2.Columns(2).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
Which came out like this:
After That i tried using this on DataGridView2.CellPainting Event
If e.Value Is Nothing Then Return
Dim s = e.Graphics.MeasureString(e.Value.ToString(), DataGridView2.Font)
If s.Width > DataGridView2.Columns(e.ColumnIndex).Width Then
Using gridBrush As Brush = New SolidBrush(DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As Pen = New Pen(gridBrush)
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
e.Graphics.DrawString(e.Value.ToString(), DataGridView2.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault)
DataGridView2.Rows(e.RowIndex).Height = CInt((s.Height * Math.Ceiling(s.Width / DataGridView2.Columns(e.ColumnIndex).Width)))
e.Handled = True
End Using
End Using
End If
which came out like this:
Tried fiddling with all properties but i seems unable to figure it out, Thank you in advance
Here's the process I followed:
Dropped a new datagridview on a form (actually I dragged it out of the datasources window, because I already had a dataset in the project)
Chose edit columns, chose a column, clicked [...] next to DefaultCellStyle for one of the string columns, set WrapMode to True
Set AutoSizeRowsMode of the DGV to AllCells
Inserted a long string into the underlying datatable the dgv was bound to:
And the result wrapped:
You can consider centering the elements in the DataGridView:
DataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
DataGridView2.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Result looks like:

How to fill a combobox in a datagridview

I have a datagrid view with 10 columns. It includes 2 checkbox columns followed by a combobox and then a number of text boxes for data entry. I do not have a database to load the combobox drop down but I do have a variable with 19 rows. I have tried a number of methods from SO but haven't been able to get this to work correctly so I can load the combobox for the user to select a value.
The code I've been using is like this. I have tried several different ways that are commented out as well...
' Build datagridview row
'
Dim t1 As New DataTable
For Each col As DataGridViewColumn In dgvMultiSelect.Columns
t1.Columns.Add(col.HeaderText)
Next
Dim dgvcb As New DataTable
dgvcb.Columns.Add("RunID", GetType(String))
For el = 0 To sRunID.Length - 1
dgvcb.Columns.Add(sRunID(el))
RunID.Items.Add(sRunID(el))
Next
' RunID.DataSource = dgvcb
' RunID.DataPropertyName = "dgvcb"
' RunID.DataSource = sRunID
' RunID.DataPropertyName = "sRunID"
'Dim chk As New DataGridViewCheckBoxColumn()
'DataGridView1.Columns.Add(chk)
'chk.HeaderText = "Check Data"
'chk.Name = "chk"
dgvMultiSelect.Rows(0).Cells(0).Value = True
The checkbox works fine (it shows as checked) and I was able to set the combobox value to show but clicking on the dropdown does nothing. I believe the data is in RunID (the column in the dgv.
Well, the answer that worked for me is: I was referencing the datagridviewCOLUMN and not a datagridviewCOMBOBOXcolumn. Thanks to Sai Kalyan Kumar Akshinthala!
' Build datagridview row
Dim dgvcc As DataGridViewComboBoxColumn
dgvcc = dgvMultiSelect.Columns("RunID")
For el = 0 To sRunID.Length - 1
dgvcc.Items.Add(sRunID(el))
dgvMultiSelect.Rows(0).Cells(2).Value = sRunID(el)
Next
Maybe next time you'll try to understand before voting down. Such a silly thing this voting down anyway. It should not reduce reputation.

Capturing an event such as mouse click in Datagridview in VB

Update 5/21/17. Thank you for the suggestion of using a Table. That was helpful. I actually figured it out. I made myinputable a global variable by declaring the Dim statement at the top and making it a Datagridview type. Now I can turn it off in the other event that I needed to do it.
I am a novice. I have created a Datagridview in VB 2015 to capture a bunch of data from the use. When the user is finished with the data entry, I want to store the cell values in my variables. I do not know how to capture any event from my dynamically created datagridview "myinputable." My code is below. Please help.
Private Sub inputmodel()
Dim prompt As String
Dim k As Integer
'
' first get the problem title and the number of objectives and alternatives
'
prompt = "Enter problem title: "
title = InputBox(prompt)
prompt = "Enter number of criteria: "
nobj = InputBox(prompt)
prompt = "Enter number of alternatives: "
nalt = InputBox(prompt)
'
' now create the table
'
Dim Myinputable As New DataGridView
Dim combocol As New DataGridViewComboBoxColumn
combocol.Items.AddRange("Increasing", "Decreaing", "Threashold")
For k = 1 To 6
If k <> 2 Then
Dim nc As New DataGridViewTextBoxColumn
nc.Name = ""
Myinputable.Columns.Add(nc)
Else
Myinputable.Columns.AddRange(combocol)
End If
Next k
' now add the rows and place the spreadsheet on the form
Myinputable.Rows.Add(nobj - 1)
Myinputable.Location = New Point(25, 50)
Myinputable.AutoSize = True
Me.Controls.Add(Myinputable)
FlowLayoutPanel1.Visible = True
Myinputable.Columns(0).Name = "Name"
Myinputable.Columns(0).HeaderText = "Name"
Myinputable.Columns(1).Name = "Type"
Myinputable.Columns(1).HeaderText = "Type"
Myinputable.Columns(2).Name = "LThresh"
Myinputable.Columns(2).HeaderText = "Lower Threshold"
'Myinputable.Columns(2).ValueType = co
Myinputable.Columns(3).Name = "UThresh"
Myinputable.Columns(3).HeaderText = "Upper Threshold"
Myinputable.Columns(4).Name = "ABMin"
Myinputable.Columns(4).HeaderText = "Abs. Minimum"
Myinputable.Columns(5).Name = "ABMax"
Myinputable.Columns(5).HeaderText = "Abs. Maximum "
Myinputable.Rows(0).Cells(0).Value = "Help"
If Myinputable.Capture = True Then
MsgBox(" damn ")
End If
End Sub
As #Plutonix suggests, you should start by creating a DataTable. Add columns of the appropriate types to the DataTable and then bind it to the grid, i.e. assign it to the DataSource of your grid, e.g.
Dim table As New DataTable
With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
DataGridView1.DataSource = table
That will automatically add the appropriate columns to the grid if it doesn't already have any, or you can add the columns in the designer and set their DataPropertyName to tell them which DataColumn to bind to. As the user makes changes in the grid, the data will be automatically pushed to the underlying DataTable.
When you're done, you can access the data via the DataTable and even save the lot to a database with a single call to the Update method of a data adapter if you wish.

How to Add Combobox in datagridview only for header?

This is my code - I want to add combobox for my second column. So that whatever value user enter in that column I can use for further operation. I have changed Items in combo intentionally.
Dim cmbHeaderCell As New ComboBox
cmbHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
cmbHeaderCell1.Visible = True
cmbHeaderCell1.Items.Add("India")
cmbHeaderCell1.Items.Add("China")
DGrdVLoadStb.Columns(1).Width = cmbHeaderCell1.Width
cmbHeaderCell1.Location = DGrdVLoadStb.GetCellDisplayRectangle(1, -1,True).Location
cmbHeaderCell1.Size = DGrdVLoadStb.Columns(1).HeaderCell.Size
cmbHeaderCell1.SelectedIndex = 0
Here I am getting the location (0, 0) from GetCellDisplayRectangle(1, -1, True) method. I have checked many related questions on stackOverflow but I didn't get perfect answer please help me. Thank you in advance.
As I wrote in comment, I managed to do this by using,
1. width of first column of datagridview adding 2 to it and
2. making it as X of my combobox loacation and Y as 2.
here is my code,
Dim cmbHeaderCell1 As New ComboBox
cmbHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
cmbHeaderCell1.Visible = True
cmbHeaderCell1.Items.Clear()
cmbHeaderCell1.Items.Add("India")
cmbHeaderCell1.Items.Add("China")
DGrdVLoadStb.Columns(1).Width = cmbHeaderCell1.Width
'Dim X As Integer = DGrdVLoadStb.GetCellDisplayRectangle(1, -1, False).Location.X
'Dim Y As Integer = DGrdVLoadStb.GetCellDisplayRectangle(1, -1, True).Location.Y
cmbHeaderCell1.Location = New Point(datagridview1.Columns(0).Width + 2, 2)
cmbHeaderCell1.Size = DGrdVLoadStb.Columns(1).HeaderCell.Size
cmbHeaderCellStressRate.DropDownStyle = ComboBoxStyle.DropDownList
DGrdVLoadStb.Controls.Add(cmbHeaderCell1)
cmbHeaderCell1.SelectedIndex = 0
If any one have better solution please post it I will accept it.

create labels on a form based on entries in datagridview vb.net

I have a form with a datagridview containing entries for an invoice. I have created a separate form that will contain the entries from the datagridview in labels. Unfortunately, I am having trouble finding out how to generate a table of labels with each label holding the value of the corresponding entry in the datagridview. I am wondering if it is possible to do this with a for next loop based on the number of rows in the datagridview. Thanks in advance for any guidance in this!
Dim lbl As New Label
With lbl
.Text = txtCaption ' value you want it to display from dgv.dr
.Top =
.Left =
.Name =
.Size =
End With
I dont know what you mean by a table of labels, so lets say you want to add them to a panel there just for them:
pnlLabels.Controls.Add(lbl)
Make that a Sub and call it from the loop processing the rows in the datagridview, passing the relevant value for its caption. Something like this:
CreateLabel(ndx as Integer, txtCaption as String)
You will need an index or counter of how many made so that some properties like .Top can be offset accordingly.
EDIT
I have no idea what your dgv or data etc looks like, but your loop would be SOMETHING like this:
pnlLabels.Controls.Clear ' might want to remove any old ones
Dim Caption as String = ""
Dim ndx As Integer = 0
For Each dr As DataGridViewRow in dgv.rows
Caption = dr.Cells(X) ' or where the data is
CreateLabel(ndx, Caption)
ndx += 1
Next