DataGridViewButtonCell button color change on click

*爱你&永不变心* 提交于 2020-01-17 14:17:45

问题


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).

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?


回答1:


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;


来源:https://stackoverflow.com/questions/37198297/datagridviewbuttoncell-button-color-change-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!