C# 从多个列中获取值并为其分配按钮属性
我有一个房间数据库和windows窗体上的6个按钮。我想根据数据库中的值更改按钮属性 例如,按钮文本应该是C# 从多个列中获取值并为其分配按钮属性,c#,sql-server,C#,Sql Server,我有一个房间数据库和windows窗体上的6个按钮。我想根据数据库中的值更改按钮属性 例如,按钮文本应该是RoomNumber,按钮背景颜色应该是RoomStatus:如果RoomStatus=0,按钮背景应该是蓝色,1=黄色,2=红色 我只能设置按钮的文本,但无法设置背景色: void getRoomValue() { con.Open(); SqlCommand cmdGR = new SqlCommand("select RoomNum, RoomStat
RoomNumber
,按钮背景颜色应该是RoomStatus
:如果RoomStatus=0
,按钮背景应该是蓝色,1=黄色,2=红色
我只能设置按钮的文本,但无法设置背景色:
void getRoomValue()
{
con.Open();
SqlCommand cmdGR = new SqlCommand("select RoomNum, RoomStatus from RoomNumber", con);
DataTable dt = new DataTable();
dt.Load(cmdGR.ExecuteReader());
con.Close();
for (int i = 0; i < dt.Rows.Count; i++)
{
string buttonName = "button" + (1 + i).ToString();
Control[] buttons = this.Controls.Find(buttonName, false);
if (buttons.Length == 1 && buttons[0] is Button)
{
Button btn = (Button)buttons[0];
btn.Text = dt.Rows[i].Field<string>("RoomNum");
}
}
}
void getRoomValue()
{
con.Open();
SqlCommand cmdGR=new-SqlCommand(“从RoomNumber中选择RoomNum、RoomStatus”,con);
DataTable dt=新的DataTable();
dt.Load(cmdGR.ExecuteReader());
con.Close();
对于(int i=0;i
您没有按钮。您有包含名称的表的标题行。我在windows窗体上有按钮,这里没有提到它们。请尝试以下操作:string status=dt.rows[I].Field(“RoomStatus”);开关(状态){case“0”:btn.BackColor=Color.Blue;break;case“1”:btn.BackColor=Color.Yellow;break;case“2”:btn.BackColor=Color.Red;break;}是的,它按我的要求工作。非常感谢你