如何将数据从数据库加载到单选按钮

时间:2015-11-28 15:30:05

标签: vb.net datagridview

我有两个单选按钮供NEW和OLD学生使用。 每当我单击表单中的datagridview时,我也想将它加载到单选按钮。 这是我的代码。

   Private Sub dg1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellContentClick
    Try
        txtProductName.Text = dg1.Item(1, e.RowIndex).Value
        dtpImport.Text = dg1.Item(2, e.RowIndex).Value
        dtpExs.Text = dg1.Item(3, e.RowIndex).Value
        txtQuantity.Text = dg1.Item(4, e.RowIndex).Value
        txtPrice.Text = dg1.Item(5, e.RowIndex).Value
        txtUnitPrice.Text = dg1.Item(6, e.RowIndex).Value
        Dim ms As New MemoryStream(changephoto(CInt(dg1.SelectedCells(0).Value)))
        PictureBox1.Image = Image.FromStream(ms)
        rbtNew.Text = dg1.Item(8, e.RowIndex).Selected
        rbtOld.Text = dg1.Item(8, e.RowIndex).Selected
    Catch ex As Exception

    End Try


End Sub

这是我的单选按钮代码,但它给了我错误的结果。

rbtNew.Text = dg1.Item(8, e.RowIndex).Selected
rbtOld.Text = dg1.Item(8, e.RowIndex).Selected

My screenshot of my forms with wrong result

Image2

1 个答案:

答案 0 :(得分:2)

因为代表“新”或“旧”学生的值是String(Varchar),
那么你需要将这个值转换为RadioButton

的布尔值
const OLDSTUDENT As String = "Old"
const NEWSTUDENT As String = "New"

Me.rbtNew.Text = NEWSTUDENT
Me.rbtOld.Text = OLDSTUDENT

Me.rbtNew.Checked = NEWSTUDENT.Equals(dg1.Item(8, e.RowIndex).Value.ToString())
Me.rbtOld.Checked = OLDSTUDENT.Equals(dg1.Item(8, e.RowIndex).Value.ToString())