How to fill a combobox in a datagridview - vb.net

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.

Related

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.

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next

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

Make particular column as combo box of DataGridView

I just want to make Data Grid. Where first and second column of data grid is for storing single value and third column as combo box.
The code that i tried is
Dim productGrid As New DataGridView
ProductGrid.Columns(0).Name = "CB"
ProductGrid.Columns(1).Name = "ProductGroup"
ProductGrid.Columns(2).Name = "Product"
Dim i As Integer
With ProductGrid
If .Rows.Count = 0 Then Exit Sub
i = 1
Dim objValueItems As New DataGridViewComboBoxCell
objValueItems.Items.Add("Server")
objValueItems.Items.Add("Standalone")
objValueItems.Items.Add("Demo")
objValueItems.Items.Add("Anywhere")
ProductGrid.Item(2, i).Value = objValueItems
End With
I am getting the error on "ProductGrid.Item(2, i).Value = objValueItems" this line. Error is " Index was out of range.
Bear in mind that all the cells of a ComboBox DGV Column have the same contents, thus you are not assigning a list of items to a given cell, but to all of them (to a column). You have to include this when adding the given column. Sample code:
Dim productGrid As New DataGridView
With productGrid
.Columns.Add("CB", "CB") 'Text-type column
.Columns.Add("ProductGroup", "ProductGroup") 'Text-type column
Dim objValueItems As New DataGridViewComboBoxColumn
With objValueItems
.Name = "Product"
.Items.Add("Server")
.Items.Add("Standalone")
.Items.Add("Demo")
.Items.Add("Anywhere")
End With
.Columns.Add(objValueItems) 'ComboBox-type column
End With

Changing from a datagridview to a simple textbox

I have a program that puts a cell of data from an excel file and writes it to a datagridview cell. However since I'm only trying to write one cell of data I'm thinking it might be a better Idea to write the data from the excel file to a simple textbox.
I'm having trouble finding out how to interface a simple textbox with an excel file. If anyone has some suggestions on how I might accomplish this I would greatly appreciate it.
Below is my current code for working with my datagridview.
'This code sample here uses a for next loop to match the excel column and rows up with the datagridview's rows and columns.
Dim rowindex As Integer
Dim columnindex As Integer
For rowindex = 1 To DataGridView3.RowCount
For columnindex = 1 To DataGridView3.ColumnCount
objworksheet3.Cells(rowindex + 3, columnindex + 1) = DataGridView3(columnindex - 1, rowindex - 1).Value
Next
Next
'This code uses the oledatadapter to pull only the cells that I want from the excel file to populate the datagridview cell.
MyCommand3 = New OleDbDataAdapter("select * from [myWorksheet$B3:B4]", MyConnection)
ds3 = New System.Data.DataSet()
MyCommand3.Fill(ds3)
For rowindex = 1 To DataGridView3.RowCount
I suggest, For rowindex = 0 To DataGridView3.RowCount -1
You can get data directly from your dataset; for example to retrieve data from the first row of the first column:
Textbox1.text = ds.Tables(0).Rows(0)(0).ToString
if you want to fetch more cells you can cycle through rows and columns