DataGridViewButtonCell按钮颜色变化点击 [英] DataGridViewButtonCell button color change on click
问题描述
我有一个 DataGridViewRow
,我以程序方式添加到一个 DataGridView
table。我想要一个单元格是一个 DataGridViewButtonCell
,所以我可以点击它。 (我意识到我可以在datagridview上找到一个用于 on_click 的处理程序,但是我想为了简化代码而保持这种方式)。
I have a DataGridViewRow
that I add to a DataGridView
"table" programatically. I want one of the cells to be a DataGridViewButtonCell
so I can click on it. (I realize I can have a handler on the datagridview for on_click but I'd like to keep this way for simplicity of code).
我想要设置DataGridViewButtonCell的forecolor / backcolor。
I want to be able to set the forecolor/backcolor of a DataGridViewButtonCell.
DataGridViewRow dgvRow = new DataGridViewRow();
dgvRow = (DataGridViewRow)dgv.Rows[0].Clone();
dgvRow.Cells[0].Value = filename;
dgvRow.Cells[0].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
DataGridViewButtonCell dgvBtn = new DataGridViewButtonCell();
dgvRow.Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvBtn.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvBtn.Style.BackColor = Color.Green; //this sets the color of the cell, not the button in the cell.
问:如何设置单元格中出现的按钮的颜色?
Q: How can I set the color of the button that appears in the cell?
推荐答案
非常像常规的按钮
,您需要使其成为 FlatStyle
按钮
更改 BackColor
:
Pretty much like with a regular Button
you need to make it a FlatStyle
Button
to change the BackColor
:
dgvBtn.FlatStyle = FlatStyle.Flat;
dgvBtn.Style.BackColor = Color.LightGreen;
这篇关于DataGridViewButtonCell按钮颜色变化点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!