C# 无法恢复到按钮设置好的BackColor?只能恢复到Control BackColor(例如灰色)的解决方案:...

 
 C# 无法恢复到按钮设置好的BackColor?
只能恢复到Control BackColor(例如灰色)的解决方案:

//定义一个颜色

Color currentbtBackColor;

 //保存当前按钮颜色

currentbtBackColor = btStopRecord.BackColor;

//恢复该BackColor颜色

btRecord.BackColor = currentbtBackColor;

 
 
 

转载于:https://www.cnblogs.com/watermarks/p/8455580.html

当然,`DataGridView.DefaultCellStyle.BackColor` 属性用于设置 DataGridView 中单元格的背景颜色。除了直接赋值,还有其他几种方法可以设置这个属性: 1. **遍历行或列并设置**: 如果你想根据特定条件设置不同行或列的背景色,你可以使用 ` DataGridViewCellStyle` 的 `ApplyTo` 方法,如下所示: ```csharp DataGridViewCellStyle style = new DataGridViewCellStyle(); style.BackColor = Color.Green; // 设置所有偶数行的背景色 for (int i = 0; i < dataGridView.Rows.Count; i += 2) dataGridView.Rows[i].DefaultCellStyle = style; // 或者只针对某一列 DataGridViewColumn column = dataGridView.Columns["YourColumnName"]; foreach (DataGridViewRow row in dataGridView.Rows) if (row.IsNewRow) continue; // 只对已存在的行操作 row.Cells[column.Index].DefaultCellStyle = style; ``` 2. **事件处理程序**: 当单元格的数据改变时,可以通过编写 `CellValueChanged` 或 `CellFormatting` 事件处理器来自定义背景颜色: ```csharp private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == /* 列索引 */ && e.Value != null && e.Value.ToString().Length > 5) // 检查条件 e.CellStyle.BackColor = Color.Green; } ``` 3. **样式表( DataGridViewCellStyleCollection)**: 创建一个样式表集合,然后根据需要添加规则,再应用到整个网格: ```csharp DataGridViewCellStyleCollection styles = new DataGridViewCellStyleCollection(); DataGridViewCellStyle greenStyle = new DataGridViewCellStyle { BackColor = Color.Green }; styles.Add(greenStyle); dataGridViewCellStyleAppliedToCells(styles, "SomeCondition"); // 自定义函数应用样式 private void dataGridViewCellStyleAppliedToCells DataGridViewCellStyleCollection styles, string condition) { foreach (DataGridViewRow row in dataGridView.Rows) if (row.Cells.Any(c => c.OwningColumn.HeaderText.Contains(condition))) // 根据标题检查 row.DefaultCellStyle = styles[0]; } ``` 以上就是设置 `DataGridView.DefaultCellStyle.BackColor` 的不同方法。选择哪种取决于你的具体需求和应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值