简体   繁体   English

将多个复选框值放入datagridview单列

[英]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. 我试图允许特定的复选框,如果选中要合并的数据并将其插入到datagridview单列中。

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? 因此,如果有人选择“ Checkbox1&Checkbox2”,如何获得datagridview列“ Drinks”以显示可乐和百事可乐?

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 我执行DropDownStyle = DropDownList并确保已选择项目的组合框

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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