I have a datagridview combo box column with Read Only property set to False.
I need to insert values to the combo box (say One, Two, Three etc.) and also I need the combo box to display a certian value stored in a variable (say dim abc as string="value")
I need the combo box to display value stored in abc when the form loads.
Please advise how to achieve these two tasks.
Thanks
Combobox-type DataGridView cells/columns are, by default, read-only (users cannot change their items). Sample code to start a DataGridView1 with a combobox-type column, populated with the values you wish:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim curCol As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn
Dim abc As String = "value"
Dim items As List(Of String) = New List(Of String)()
items.Add("One")
items.Add("Two")
items.Add("Three")
items.Add(abc)
curCol.DataSource = items
DataGridView1.Columns.Add(curCol)
End Sub
Related
I am a fairly new to visual studio. I have 2 forms with dropdown cb_CBOX1 & dropdown list cb_CBOX2.
I want user to add data to cb_CBOX1 and have the data be inserted in alphabetical order in cb_CBOX1 dropdown and be inserted in alphabetical order in cb_CBOX2 dropdown list also.
I'm trying to use the below statement. TIA
cb_CBOX2.Items.Add(cb_CBOX1.Text)
Update 2017-11-07:
I have 2 forms both have combobox with drop down list. I want user to insert data to cb_CBOX1 and have the data added alphabetically to the cb_CBOX1 drop down and added alphabetically to cb_CBOX2 drop down. When the user types in data in cb_CBOX1, they will click button1 to call the Add function.
My code:
Private Sub button1.Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
cb_CBOX2.Items.Add (cb_CBOX1.Text)
End Sub
Extract the items, add the new item, sort, then apply the items back. It's fairly simple with Linq.
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
addToAndSortComboBox(cb_CBOX1, cb_CBOX1.Text)
addToAndSortComboBox(cb_CBOX2, cb_CBOX1.Text)
cb_CBOX1.Text = "" ' optional
End Sub
Private Sub addToAndSortComboBox(cb As ComboBox, value As String)
Dim items = cb.Items.Cast(Of String).Concat({value}).OrderBy(Function(v) v).ToList()
cb.Items.Clear()
cb.Items.AddRange(items.ToArray())
End Sub
Just a comment on the design: it doesn't seem very intuitive. I feel like the input should be done in a TextBox with the ComboBoxes having ComboBox.DropDownStyle = DropDownList.
It sounds like you want to be able to add data to a list, Whatever the user types into combobox1, will appear on combobox2 on a different window?
I'd suggest binding your combo boxes to a list, and then setting the item-sources of both combo-boxes to this list.
Create the list, and reference it in a class so you can access it from a different window...
Dim comboboxitemlist As New List(Of String)
Bind your list to CB2...
cb_CBOX2.itemssource = comboboxitemlist
In a button or however you are adding...
comboboxitemlist.add(cb_cbox1.text)
Have datagridview which contains 4 columns created manually in datagridview creator. 1st, 2nd and 3rd columns are just textbox columns but the last one is marked as comboboxcolumn. On my form i have button, when user clicks it new row is add to datagridview. For first three columns data is filled up from some variables, and the last combobox column should be filled up for user from datasource so he could select his value out of it. The problem is i have problems with this combobox column and so far couldn't fill it in. This is my actual code
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.Items.AddRange(dt.AsEnumerable().Select(Function(s) s.Field(Of String)("Name")).ToArray())
try
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
Tried also this:
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.DataSource = dt
cbo.ValueMember = "Id"
cbo.DisplayMember = "Name"
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
In both cases every time user clicks button row is added to datagrid but last column's combobox is empty. How to solve that?
Try to add this line for asociate your comboBox to the wanted column :
dgvMaterials.Columns.Insert(3, cbo)
I'm trying to set the length of a Combo Box control in the most efficient way possible. By length, I mean the number of items in the items collection of the Combo Box control.
This is the best attempt I have:
Dim cboNew As New ComboBox
For i As Integer = 0 To cboSelection.Items.Count - 1
cboNew.Items.Add(cboSelection.Items(i))
Next
cboSelection is another Combo Box control I have, I am pretty much trying to set the length of cboSelection to cboNew with one line of code (If cboSelection has 5 items, then set cboNew to have 5 items). I feel as if I have done this before, but have forgotten how.
If you are willing to use the DataSource property, you could do something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = New String() {"tony", "bruce", "clark"}
ComboBox2.DataSource = ComboBox1.DataSource
End Sub
I am using a DataGridView with an ArrayList. When a user clicks a button, I want the values of the first column (there will be 4 columns total, each having its own ArrayList) of the DataGridView to be written to an ArrayList. I am using an ArrayList because the number of rows in the Datagridview can vary, so the array size cannot be static. The code that I have written, actually searched and got help online with is "almost" working. It will write the values of the first column to the arraylist, but I have to click on the button twice. It will only update the ArrayList on the second click of my button. What needs to change with my code? Thanks from a newbie!
Private Sub btnPrintArray_Click(sender As Object, e As EventArgs) Handles btnPrintArray.Click
Dim message = String.Join(Environment.NewLine, lftMtrAccelRates.ToArray())
lftMtrAccelRates.Clear() 'clears ArrayList
rchTxtBox.Clear() 'Clears rich text box that has array element values in it
For Each d In LftMtr_Data_Grid.Rows.Cast(Of DataGridViewRow)()
Dim num As Integer
If Int32.TryParse(d.Cells(0).Value, num) Then
lftMtrAccelRates.Add(num)
End If
Next
rchTxtBox.Text = message
End Sub`
One possibility:
Private values As New List(Of Integer)
Private Sub btnPrintArray_Click(sender As Object, e As EventArgs) Handles btnPrintArray.Click
values.Clear()
Dim num As Integer
For Each d As DataGridViewRow In LftMtr_Data_Grid.Rows
If Int32.TryParse(d.Cells(0).Value, num) Then
values.Add(num)
End If
Next
rchTxtBox.Lines = values.ConvertAll(Function(x) x.ToString).ToArray
End Sub
how i can change the item of combo box that is a column of data grid view according to the combobox that is present in the form
Dim productGrid as DataGridView
Dim objProductGroup As New DataGridViewComboBoxColumn
With productGroup
.HeaderText = "ProductGroup"
.Name = "ProductGroup"
.ReadOnly = True
.Items.Add("Server")
.Items.Add("Standalone")
End With
.Columns.Add(objProductGroup)
I have to select the objProductGroup combo box as per combo box that is on the form
dim box1 as ComboBox
box1..Items.Add("Server")
box1.Items.Add("Standalone")
When i will select the box1 item Server then objProductGroup comboBox should automatically updated.
The following code will change your DataGridView's CurrentRow Column "ProductGroup" to the value you selected in box1. I am not sure if you were trying to set ALL of the rows to the value in the combobox or just the current row.
In any case, you may want to test if the CurrentRow actually has any cells. For example:
If Not productGrid.CurrentRow Is Nothing Then [Execute the value changed]
For the sake of making it work after I selected a row, this is the code I used:
Private Sub box1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles box1.SelectedIndexChanged
productGrid.CurrentRow.Cells("ProductGroup").Value = box1.SelectedItem
End Sub