如果要检查要合并的数据并将其插入datagridview单列,我试图允许特定的复选框。
表单是按如下方式添加列;
table.Columns.Add("Drinks", Type.GetType("System.Int32"))
然后
Checkbox 1 = Coke
Checkbox 2 = Pepsi
Checkbox 3 = Fanta
这是该类的代码:
Dim Drinks As String
每个复选框都包含以下代码及其文本;
Drinks = "Coke"
生成信息的按钮如下:
table.Rows.Add(Drinks.ToString)
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataGridView1.RowTemplate.Height = 100
DataGridView1.AllowUserToAddRows = False
DataGridView1.DataSource = table
因此,如果有人选择' Checkbox1& Checkbox2'我怎样才能获得datagridview专栏' Drinks'显示焦炭和百事可乐?
答案 0 :(得分:1)
创建字符串的基础是创建一个列表(Of CheckBox),用它来查询检查哪些CheckBox控件,例如。
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
组合框,我做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
变化,我们不做初步选择。
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