0

I'm trying to allow specific checkbox's if checked for the data to be merged and to be inserted into the datagridview single column.

The form is adding column as follows;

table.Columns.Add("Drinks", Type.GetType("System.Int32"))

Then

Checkbox 1 = Coke
Checkbox 2 = Pepsi
Checkbox 3 = Fanta

This is the code for the class:

Dim Drinks As String

Each of the checkbox has the following codes along with their text;

Drinks = "Coke"

The button to generate the information is as follows;

  table.Rows.Add(Drinks.ToString)
    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGridView1.RowTemplate.Height = 100
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.DataSource = table

Therefore if someone selects 'Checkbox1 & Checkbox2' how can I get datagridview column 'Drinks' to show coke & pepsi?

1 Answer 1

1

The basics for creating your string is to create a list(Of CheckBox), use it to query which CheckBox controls are checked e.g.

Public Class Form1
    Public checkBoxList As List(Of CheckBox)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim values As String = "Drinks " &
            String.Join(" ", checkBoxList _
            .Where(Function(cb) cb.Checked).Select(Function(cb) cb.Text))

        ' use values for placing into your DataGridView

    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        checkBoxList = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3}
    End Sub
End Class

ComboBoxes which I do DropDownStyle = DropDownList and ensure a item is selected

Public Class Form1
    Private comboBoxList As List(Of ComboBox)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim values As String = "Food " & String.Join(" ",
            comboBoxList.Select(Function(cb) cb.Text))
        Label1.Text = values
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        comboBoxList = New List(Of ComboBox) From
            {
                ComboBox1,
                ComboBox2,
                ComboBox3,
                ComboBox4
            }
        comboBoxList.ForEach(Sub(cb)
                                 cb.DropDownStyle = ComboBoxStyle.DropDownList
                                 cb.SelectedIndex = 0
                             End Sub)
    End Sub
End Class

Variation, we don't make an initial selection.

Public Class Form1
    Private comboBoxList As List(Of ComboBox)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim values As String = "Food " & String.Join(" ",
            comboBoxList.Where(Function(cb) Not String.IsNullOrWhiteSpace(cb.Text)) _
            .Select(Function(cb) cb.Text))

        Label1.Text = values
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        comboBoxList = New List(Of ComboBox) From
            {
                ComboBox1,
                ComboBox2,
                ComboBox3,
                ComboBox4
            }
        comboBoxList.ForEach(Sub(cb)
                                 cb.DropDownStyle = ComboBoxStyle.DropDownList
                             End Sub)
    End Sub
End Class
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. What if i had various types of 'Drinks' column headers? Example Combobox 4,5,6 are food
First off you should had asked a new question but I did modify my answer to include two variations for your new question.
You are amazing! I will give this a try shortly. Appreciate the time and effort put into the response.
Quick question. When using code Public checkBoxList As List(Of CheckBox) in Public Class. What would i use for the following: table.Rows.Add(checkBoxList.) as if i us checkboxlist.tostring it gives me the following in the datagridview System.Collections.Generic.List1[System.Windows.Forms.CheckBox]`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.