-7

有人可以帮忙吗?

如何使用数据库数据更改按钮背景色。例如,当我将特定按钮设置为保留或已预订状态时,背景颜色应变为红色。

4

2 回答 2

1

您需要使用SQLConnection对象或适当的类建立与数据库的连接,具体取决于您的数据库。

例如:

bool isReserved;
using (SqlConnection connection = new SqlConnection(connectionString)
using (SqlCommand command = new SqlCommand("SELECT isReserved FROM YourTable WHERE BookingId = 1", connection))
{
    connection.Open();  
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            isReserved = (bool)reader["isReserved"];
        }
    }
}

然后,您可以使用该BackColor属性。

if (isReserved) {
    Button1.BackColor = Color.Red; 
}
于 2013-06-24T09:57:40.530 回答
0

假设id您的按钮是Button1,那么您可以执行以下操作:

Button1.BackColor = 颜色.红色

于 2013-06-24T09:56:54.340 回答