[英]Make a radio button checked depending on the value in database
Through this code I am retreiving the values from database in edit form: 通过此代码,我以编辑形式从数据库检索值:
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;
}
}
Now I am having trouble with the radiobuttons
if the val
is Yes
in the database then radiobutton1
should be checked. 现在,如果数据库中的
val
为Yes
是Yes
,则我在radiobuttons
上遇到了麻烦,那么应该检查radiobutton1
。
If No
is in database then radiobutton2
should be selected. 如果数据库中
No
,则应选择radiobutton2
。 But the syntax is an showing error, can anyone please help me? 但是语法是一个显示错误,任何人都可以帮助我吗?
Syntax for setting checked propery of radiobutton is 设置单选按钮的已检查属性的语法为
radioButton1.Checked = true;
So ur code would look like 所以我们的代码看起来像
if (val == "Yes")
{
radioButton1.Checked=true;
radioButton2.Checked=false;
}
else if (val == "No")
{
radioButton2.Checked=true;
radioButton1.Checked=false;
}
Just update Checked
property of the radiobuttons: 只需更新单选按钮的
Checked
属性:
radioButton1.Checked = (val == "Yes);
radioButton2.Checked = !radioButton1.Checked;
Nitin Varpe's answer is great but there is one more thing I would improve on your code and that is indexing columns by it's Nitin Varpe的答案很好,但是我还需要在您的代码上做更多改进,那就是对列进行索引
put this code in class: 将此代码放在类中:
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;
}
}
So when you call for value it will look like this: 因此,当您呼吁价值时,它将看起来像这样:
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();
Indexing columns might lead messing up whole code when changing the table structure. 在更改表结构时,索引列可能会导致整个代码混乱。 Hope it helped a little.
希望它能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.