Datagridview将Datetime转换为String

时间:2013-02-20 10:07:14

标签: vb.net datagridview typeconverter

如何将datagridview项目(日期时间格式)转换为字符串。

这是我的代码:

ComboBox1.Text = DatagridView1.Item(4, 1).Value.ToString()

有人能帮助我吗?这是完整的代码:

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.ToString
    cmbTA.Text = y.ToString
    txtTrainName.Text = DataGridView1.Item(6, i).Value
    cmbFlag.Text = DataGridView1.Item(7, i).Value

End Sub

1 个答案:

答案 0 :(得分:0)

如果您要将Value显示为ComboBox,则必须将该属性设置为DropDownStyle = DropDown,并且您不需要i = DataGridView1.CurrentRow.Index来获取currentRowIndex } DataGridView

中的CellClick event
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick 

   ComboBox1.Text = DatagridView1.Item(4, e.RowIndex).Value.ToString()

End Sub