-1

和不能得到xcmbTS和y作为文本cmbTA

datagridview1.item(4,i)anddatagridview1.item(5,i)是日期类型。我想将其转换为字符串,以便可以将其combobox作为文本放在我的位置上。我combobox的是cmbTScmbTA

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        btnEdit.Visible = True
        Dim i As Integer
        i = DataGridView1.CurrentRow.Index
        Dim x As String = CStr(DataGridView1.Item(4, i).Value.ToString)
        Dim y As String = CStr(DataGridView1.Item(5, i).Value.ToString)

        MsgBox(x)
        MsgBox(y)

        txtTrainID.Text = DataGridView1.Item(0, i).Value
        txtRouteID.Text = DataGridView1.Item(1, i).Value
        txtLine.Text = DataGridView1.Item(2, i).Value
        cmbLine1.Text = DataGridView1.Item(3, i).Value
        cmbLine2.Text = DataGridView1.Item(3, i).Value
        cmbLine3.Text = DataGridView1.Item(3, i).Value
        cmbTS.Text = x
        cmbTA.Text = y
        txtTrainName.Text = DataGridView1.Item(6, i).Value
        cmbFlag.Text = DataGridView1.Item(7, i).Value

    End Sub
4

1 回答 1

1

要添加到组合框,您需要使用 cmbBox.Items.Add,所以它是

cmbTS.items.Add(x)
cmbTA.items.Add(y)

如果组合框中只有一项并且希望它显示,则需要设置 selectedindex 属性,例如:

cmbTS.SelectedIndex = 0
cmbTA.SelectedIndex = 0

此外,在声明 X 和 Y 时,您正在将字符串转换为字符串,因此您实际上并不需要 CStr ,因为最后有 .tostring 。

编辑 - 因为只有一个日期,DateTimePicker 不是比 ComboBox 更好的选择吗?

于 2013-02-21T03:01:39.340 回答