VB.NET选中datagrid复选框时,使用Datagrid第一列填充文本框

时间:2014-07-22 04:32:38

标签: vb.net

我是vb.net的新手。我的问题可能很简单,但我很难找到解决方案。 我有一个数据网格(名为DatagridView1),第一列名为Select_Column,为复选框列,下一列为LossTypes

当用户选中该复选框时,我想在文本框中显示LossTypes。它也适用于多选,因为我的数据网格是多选的

由于 萨蒂什南比亚

2 个答案:

答案 0 :(得分:1)

类似(更容易理解):

Dim result as String = ""
For x As Integer = 0 to DataGridView1.Rows.Count - 1
  If DataGridView1.Rows(x).Cells("Select_Column").Value Then
     result &= DataGridView1.Rows(x).Cells("LossTypes").Value & ","
  End If
Next
if result.length > 0 then 
   Textbox1.Text = Microsoft.VisualBasic.Left(result, result.length-1)
else
   Textbox1.Text = ""
end

或使用Linq

Textbox1.Text = Join((From C DataGridView1.Rows Where C.Cells("Select_Column").Value Select C.Cells("LossTypes").Value).ToArray, ",")

答案 1 :(得分:1)

试试这个

Dim listSelected As New List(String)

Private Sub dgv_CellValueChanged()  'DataGridView Cell Value Changed Event
    IF (CBool(dgv.CurrentRow.Cells("CheckBoxColumnNamne").Value) Then
        IF NOT listSelected.Contains(dgv.CurrentRow.Cells("LossTypes").Value.ToString()) Then
            listSelected.Add(dgv.CurrentRow.Cells("LossTypes").Value.ToString())
        End IF
    Else
        IF listSelected.Contains(dgv.CurrentRow.Cells("LossTypes").Value.ToString()) Then
            listSelected.Remove(dgv.CurrentRow.Cells("LossTypes").Value.ToString())
        End IF
    End If
    textBox1.Text = String.Join(",", listSelected.ToArray())
End Sub