简体   繁体   中英

Multiple checkbox value into datagridview single column

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM