C# 根据数据库中的值选中单选按钮

C# 根据数据库中的值选中单选按钮,c#,oledb,C#,Oledb,通过此代码,我以编辑形式从数据库中检索值: OleDbCommand Comm1 = new OleDbCommand("select image1,image2,image3,image4,measurement,property_purpose,bedrooms,bathrooms,furnishing,property_price,property_price_per_mu,existing_customer from tb_property where property_id = ?"

通过此代码,我以编辑形式从数据库中检索值:

OleDbCommand Comm1 = new OleDbCommand("select image1,image2,image3,image4,measurement,property_purpose,bedrooms,bathrooms,furnishing,property_price,property_price_per_mu,existing_customer from  tb_property where property_id = ?", con);
Comm1.Parameters.AddWithValue("property_id", txt_prop_id.Text);
OleDbDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read())
       {
          txt_image1.Text = DR1.GetValue(0).ToString();
          txt_image2.Text = DR1.GetValue(1).ToString();
          txt_image3.Text = DR1.GetValue(2).ToString();
          txt_image4.Text = DR1.GetValue(3).ToString();
          combo_measure.Text = DR1.GetValue(4).ToString();
          combo_purpose.Text = DR1.GetValue(5).ToString();

          combo_bedrooms.Text = DR1.GetValue(6).ToString();
          combo_bathrooms.Text = DR1.GetValue(7).ToString();
          combo_furnishing.Text = DR1.GetValue(8).ToString();
          txt_price.Text = DR1.GetValue(9).ToString();
          txt_price_per_mu.Text = DR1.GetValue(10).ToString();
          var val = DR1.GetValue(11).ToString();
          if (val == "Yes")
          {
              radioButton1.Checked;
          }
          if (val == "No")
          {
              radioButton2.Checked;
          }
       }
现在我的
单选按钮有问题
如果数据库中的
val
Yes
,则应检查
radiobutton1


如果数据库中有
No
,则应选择
radiobutton2
。但是语法是一个显示错误,有人能帮我吗?

设置radiobutton选中属性的语法是

radioButton1.Checked = true;
所以你的代码看起来像

          if (val == "Yes")
          {
              radioButton1.Checked=true;
              radioButton2.Checked=false;
          }
          else if (val == "No")
          {
              radioButton2.Checked=true;
              radioButton1.Checked=false;
          }

设置单选按钮的选中属性的语法为

radioButton1.Checked = true;
所以你的代码看起来像

          if (val == "Yes")
          {
              radioButton1.Checked=true;
              radioButton2.Checked=false;
          }
          else if (val == "No")
          {
              radioButton2.Checked=true;
              radioButton1.Checked=false;
          }

只需更新单选按钮的
Checked
属性:

  radioButton1.Checked = (val == "Yes);
  radioButton2.Checked = !radioButton1.Checked;

只需更新单选按钮的
Checked
属性:

  radioButton1.Checked = (val == "Yes);
  radioButton2.Checked = !radioButton1.Checked;

Nitin Varpe的答案很好,但我还需要改进您的代码,那就是根据

将此代码放入课堂:

public static class DataExtensions
    {
    public static string GetSafeString(this OleDbDataReader reader, string colName)
    {

        if (reader[colName] != DBNull.Value)
            return reader[colName].ToString();
        else
            return string.Empty;
    }
}
因此,当您调用value时,它将如下所示:

con.Open();
OleDbDataReader DR1 = Comm1.ExecuteReader();

if (DR1.Read())
{

   textBox1.Text = (DataExtensions.GetSafeString(DR1, "COLUMN"));
   var val = (DataExtensions.GetSafeString(DR1, "COLUMN"));

   if (val == "Yes")
   {
       radioButton1.Checked;
   }
   if (val == "No")
   {
       radioButton2.Checked;
   }
}

con.Close();
在更改表结构时,索引列可能会导致整个代码混乱。
希望对你有点帮助

Nitin Varpe的答案很好,但我还要改进您的代码,那就是根据它的

将此代码放入课堂:

public static class DataExtensions
    {
    public static string GetSafeString(this OleDbDataReader reader, string colName)
    {

        if (reader[colName] != DBNull.Value)
            return reader[colName].ToString();
        else
            return string.Empty;
    }
}
因此,当您调用value时,它将如下所示:

con.Open();
OleDbDataReader DR1 = Comm1.ExecuteReader();

if (DR1.Read())
{

   textBox1.Text = (DataExtensions.GetSafeString(DR1, "COLUMN"));
   var val = (DataExtensions.GetSafeString(DR1, "COLUMN"));

   if (val == "Yes")
   {
       radioButton1.Checked;
   }
   if (val == "No")
   {
       radioButton2.Checked;
   }
}

con.Close();
在更改表结构时,索引列可能会导致整个代码混乱。
希望对你有点帮助

对于两个if语句使用
else if
没有意义对于两个if语句使用
else if