将日期转换为字符串然后添加到

时间:2013-02-21 01:37:54

标签: vb.net winforms date datagridview

cmbTScmbTA无法将 x y 作为文字

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

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

1 个答案:

答案 0 :(得分: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更好的选择吗?