如何使用C#从数据库中检索单选按钮值

时间:2016-06-20 20:03:16

标签: c# radio-button groupbox

我有一个包含单选按钮的组框,例如。

  

o等级1

     

o等级2

如何从数据库加载值并检查我的GUI上的无线电按钮?

private void button_clone_Click(object sender, EventArgs e)
{
    try
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "SELECT * from PPAPdatabase where [PSW ID]=" + txt_c_PSW_ID.Text + "";
        OleDbDataReader dr = null;
        dr = command.ExecuteReader();                
        while (dr.Read())
        {
            comboBox_PPAP.Text = (dr["Reason"].ToString());
            checkedListBox_prodline.Text = (dr["Production Line"].ToString());                    
            checkedListBox_owner.Text = (dr["Owner"].ToString());              
            txt_comment.Text = (dr["Comment"].ToString());                                                          
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error has occurred: " + ex.Message,
                "Important Note",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
    }
    finally
    {
        connection.Close();
    }   

提前致谢!

1 个答案:

答案 0 :(得分:1)

假设您的单选按钮被称为radioButton1,而您引用的列将值存储为10,那么您可以这样做:

radioButton1.Checked = row["PPAP"].ToString() == "1";