我正在尋找一種方法,通過單擊它來設定僅針對 1 個單元格的顏色。
我建立了一個 Canteen Voter,您可以每天在 4 個選單中進行選擇。
我的 DataGridView 連接到一個資料庫,它從中接收所有資訊,當您單擊一個單元格時,它也會存盤在登錄用戶的資料庫中。
現在我希望能夠在單擊時單獨為單元格著色。
到目前為止,我只找到了 SelectedCells 屬性,但這實際上只是在這些單元格再次失去焦點時為“SelectedCells”設定顏色,它們又是白色的。
我希望他們能夠保持這種顏色,讓用戶更清楚他剛剛選擇了哪道菜。

uj5u.com熱心網友回復:
你應該設定DataGridViewCell.Style屬性。
看看這個例子
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex == dataGridView1.NewRowIndex)
{
return;
}
var dataGridViewCellStyle =
new DataGridViewCellStyle(dataGridView1.DefaultCellStyle)
{
BackColor = Color.Gold
};
dataGridView1[e.ColumnIndex, e.RowIndex].Style = dataGridViewCellStyle;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485553.html
