如何获得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
...全文
102 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

110,546

社区成员

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

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

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