如何获得DataGridViewCurrentCell中内容的前景颜色?

cnjack 2011-12-01 04:02:42
运行环境:Windows XP(SP3)+vs.net2005+Crystal Reports XI

代码如何:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}

private void Form5_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id", typeof(String));
dt.Columns.Add(dc);
dc = new DataColumn("name", typeof(String));
dt.Columns.Add(dc);
for (int i = 0; i <= 20; i++)
dt.Rows.Add(i, "row" + i);
this.dataGridView1.DataSource = dt;
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
return;
if (e.RowIndex % 2 == 0 && e.ColumnIndex == 1)
e.CellStyle.ForeColor = Color.Blue;
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("CurrentCell.InheritedStyle.ForeColor:" + this.dataGridView1.CurrentCell.InheritedStyle.ForeColor.Name
+ "\r\n\r\nCurrentCell.OwningColumn.InheritedStyle.ForeColor:" + this.dataGridView1.CurrentCell.OwningColumn.InheritedStyle.ForeColor.Name
+ "\r\n\r\nCurrentCell.OwningColumn.DefaultCellStyle.ForeColor:" + this.dataGridView1.CurrentCell.OwningColumn.DefaultCellStyle.ForeColor.Name);
}
}
}
说明:DataGridView所有属性均为默认值。

当我选择有改变颜色的DataGridViewCell后,点击按钮,获得的颜色却没有我设置的颜色Blue
...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnjack 2011-12-02
  • 打赏
  • 举报
回复
谢谢sdl2005lyx。
改成以下可以了:
this.dataGridView1.CurrentCell.InheritedStyle.ForeColor.Name

this.dataGridView1.CurrentCell.Style.ForeColor//默认颜色为Color.Enpty

代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}

private void Form5_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id", typeof(String));
dt.Columns.Add(dc);
dc = new DataColumn("name", typeof(String));
dt.Columns.Add(dc);
for (int i = 0; i <= 20; i++)
dt.Rows.Add(i, "row" + i);
this.dataGridView1.DataSource = dt;
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
return;
if (e.RowIndex % 2 == 0 && e.ColumnIndex == 1)
{
//e.CellStyle.ForeColor = Color.Blue;
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
}
}

private void button1_Click(object sender, EventArgs e)
{
//以下两种情况都可以
MessageBox.Show("CurrentCell.OwningColumn.InheritedStyle.ForeColor:" + this.dataGridView1.CurrentCell.OwningColumn.InheritedStyle.ForeColor.Name
+"\r\n\r\nCurrentCell.InheritedStyle.ForeColor:" + this.dataGridView1.CurrentCell.InheritedStyle.ForeColor.Name
//+ "\r\n\r\nCurrentCell.OwningColumn.DefaultCellStyle.ForeColor:" + this.dataGridView1.CurrentCell.OwningColumn.DefaultCellStyle.ForeColor.Name
+ "\r\n\r\nCurrentCell.CurrentCell.Style.ForeColor:" + this.dataGridView1.CurrentCell.Style.ForeColor
);
}
}
}
xiongxyt2 2011-12-02
  • 打赏
  • 举报
回复
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Red;
Color col=dgv.CurrentCell.Style.ForeColor;
}


}
sdl2005lyx 2011-12-02
  • 打赏
  • 举报
回复
改成:DataGridViewCell.Style
内容概要:本文围绕“【SCUC】N-1故障集+安全约束机组组合研究(Matlab代码实现)”展开,系统探讨电力系统在N-1故障场景下的安全约束机组组合(SCUC)问题,旨在实现系统安全性与运行经济性的协同优化。研究通过构建包含机组启停、出力计划、潮流约束、备用容量及N-1安全准则的数学模型,结合混合整数线性规划等优化算法,利用Matlab平台完成仿真求解,并引入YALMIP等建模工具提升建模效率。文档提供了完整的代码实现方案与测试案例,涵盖故障集的生成与处理方法,帮助用户掌握鲁棒性和分布鲁棒优化的基本思想,适用于电力系统安全校核与调度决策支持。; 适合人群:电力系统及相关专业的研究生、从事电力调度、能源优化的科研人员及工程技术人员。; 使用场景及目标:① 掌握N-1安全准则在机组组合的建模方法与实现流程;② 开展安全约束机组组合(SCUC)与安全约束经济调度(SCED)的仿真分析;③ 利用Matlab/YALMIP快速搭建电力系统优化模型并进行求解与验证。; 阅读建议:建议读者结合文档内容与网盘提供的完整资源(含代码与数据),动手实践建模与调试过程,重点关注约束条件的设定、故障场景的枚举与处理策略,同时参考相关文献深化对鲁棒优化与分布鲁棒方法的理解。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧