Make particular column as combo box of DataGridView - vb.net

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

Related

How toI convert a Data gridview column to a list of strings?

I'm trying to convert the first column from a Data Grid View to a list of string, then search through the list and highlight the corresponding row in red.
This is what I currently have
Dim search As String = txtsearch.Text
Dim lsttitlesort As New List(Of String)()
For i = 0 To dgvbooks.Rows.Count - 1
lsttitlesort.Add(dgvbooks(0, i).Value)
Next
For i As Integer = 1 To lsttitle.Count - 1
If lsttitlesort(i).ToLower.Contains(search.ToLower) Then
dgvbooks.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Next
The problem is that when I try to run it, it crashes and displays an error message saying
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I'm new to Data Grid Views: can you explain me how to fix it?
You can search directly on the DataGridView without using a list:
Dim search As String = txtsearch.Text
'iterate through all rows of the DataGridView to search the value.
For Each dgrBook As DataGridViewRow In dgvbooks.Rows
'you can skip the new row at the end (if available).
If dgrBook.IsNewRow Then
Continue For
End If
'check if first column contains the search value.
'if search value found on first column set the background color to red,
'otherwise reset the background to white.
If CStr(dgrBook.Cells(0).Value & "").ToLower.Contains(search.ToLower) Then
dgrBook.DefaultCellStyle.BackColor = Color.Red
Else
dgrBook.DefaultCellStyle.BackColor = Color.White
End If
Next

ComboBox in DataGridView how to populate another combobox in the second column

Good evening, I have a problem on populating records in DataGridView using DataGridComboBox.
Here's what I wanted but I cant get.
I created DataGridComboBoxColumn in DataGridView1 with 2 Items inside the ComboBox DropDown -"Machine","Materials". If I select the "Machine" in ComboBox column. It will add another ComboBoxColumn into next cell with 2 items - "Machine 1", "Machine 2". Also if I select "Materials" it will change the items in "Material 1" and "2".
Anyone can help me with this much appreciated. Thanks.
Try like this code.
Dim sourceGrid As DataGridView = Me.DataGridView1
Dim targetGrid As DataGridView = Me.DataGridView2
Dim targetRows = New List(Of DataGridViewRow)
For Each sourceRow As DataGridViewRow In sourceGrid.Rows
If (Not sourceRow.IsNewRow) Then
Dim targetRow = CType(sourceRow.Clone(), DataGridViewRow)
For Each cell As DataGridViewCell In sourceRow.Cells
targetRow.Cells(cell.ColumnIndex).Value = cell.Value
Next
targetRows.Add(targetRow)
End If
Next
targetGrid.Columns.Clear()
For Each column As DataGridViewColumn In sourceGrid.Columns
targetGrid.Columns.Add(CType(column.Clone(), DataGridViewColumn))
Next

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.

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

VB2010 read data from .txt file to datagridview

I have a datagridview with 4 columns. The first 3 columns is DataGridViewTextBoxColumn while the last column is DataGridViewComboBoxColumn.
I have my .txt file and inside of it is:
Joshua,5/8/14,Paid,Yes
John Kevin,5/8/14,Paid,No
Then i have my code:
Dim TextFieldParser1 As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\1\Desktop\students.txt")
TextFieldParser1.Delimiters = New String() {","}
While Not TextFieldParser1.EndOfData
Dim Row1 As String() = TextFieldParser1.ReadFields()
If DataGridView1.Columns.Count = 0 AndAlso Row1.Count > 0 Then
Dim i As Integer
For i = 0 To Row1.Count - 1
DataGridView1.Columns.Add("Column" & i + 1, "Column" & i + 1)
Next
End If
DataGridView1.Rows.Add(Row1)
End While
My code work like this:
Joshua,5/8/14,Paid,Yes
"Joshua" will be displayed in first column.
"5/8/14" will be displayed in second column
"Paid" will be displayed in third column
"Yes" and "no" will be displayed in fourth column (combo box) and there are options
All columns are working in except on the fourth column. There is an error:
The following exception occured in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.
How can i fix it? I want the output that if the text in .txt file is "Yes" in the combo box it will auto select Yes, if "No" in .txt file it will select no in the output.