DataGridView如何实现两列数值对比后没,将相同结果的行改变颜色 请教一下,两列数据对比后,将相同结果的行改变颜色。麻烦了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你个参考, /// <summary> /// 颜色设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e != null && e.RowIndex > -1) { DataGridViewRow dr = this.dataGridView.Rows[e.RowIndex]; int statusCode = Convert.ToInt32(dr.Cells["IntStatusCode"].Value); if (statusCode == 4) { dr.DefaultCellStyle.ForeColor = Color.Gray; } if (statusCode == 8) { dr.DefaultCellStyle.ForeColor = Color.Gold; } if (statusCode == 12 || statusCode == 16) { dr.DefaultCellStyle.ForeColor = Color.Brown; } if (statusCode == 20) { dr.DefaultCellStyle.ForeColor = Color.Purple; } if (statusCode == 24 ||statusCode == 28) { dr.DefaultCellStyle.ForeColor = Color.Red; } } this.dataGridView.Columns["IntStatusCode"].Visible = false; } 可以帮忙看看我要怎么写啊??下面代码都不能用.int x=Convert.ToInt32 (DataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value);int y = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Safekeep"].Value);if (x == y) { DataGridView1.DefaultCellStyle.ForeColor = Color.Red; } 补充,上面是写在CellFormatting事件里的 if (x == y) { foreach(DataGridViewCell cell in DataGridview1.rows[e.RowIndex]) { cell.Style .ForeColor =color.red//这样行不 } } foreach 不行.唉..错误如下:错误 2 “System.Windows.Forms.DataGridViewRow”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.Windows.Forms.DataGridViewRow”类型的变量 F:\VS 2005 Projects\KC\KC\WinMain.cs 292 16 KC foreach(DataGridViewCell cell in DataGridview1.rows[e.RowIndex].Cells) 还是没有反应,不知道是不是我写错事件了??帮忙看一下〉??private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { int x = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value); int y = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Safekeep"].Value); if (x <= y) { foreach (DataGridViewCell cell in DataGridView1.Rows[e.RowIndex].Cells) { DataGridView1.DefaultCellStyle.ForeColor = Color.Red; } } } 汗,我只针对7楼看了下,没看题目1.LZ说的是相等,怎么后面就成了x <= y了2.符合条件的行变色,哪里用得上foreach再说,DataGridView1.DefaultCellStyle.ForeColor = Color.Red;也和foreach没关系啊 if (x <= y) DataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red; //这句没亲自调试,LZ先试试 相等跟<= 差不多吧。后来我想这样比较合理。新人很多都不懂,,您写的这句测试没有反应不知道是不是我那边int格式的问题。。 private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { string strOne=DataGridView1["Quantity",e.RowIndex].Value.ToString(); string strTwo=DataGridView1["Safekeep",e.RowIndex].Value.ToString(); if(strOne==strTwo) { DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red; }}//以上在CellFormatting事件里试验一下吧,另外需要注意的是空值null的情况需要单独处理,在此我就不多说了,希望你试验OK 你要的功能貌似和我之前用到的 合并单元格的功能很类似http://blog.csdn.net/zerocold_1986/archive/2008/04/14/2291947.aspx可以参考下。 CellFormatting?我做这个功能用的是CellValueChanged 15楼的可以哦..不过好像不能转成Int类型去比较。 因为我想变成<=。 这样比较合理。 //很简单,把string转换成int就可以了private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { int One=int.Parse (DataGridView1["Quantity",e.RowIndex].Value.ToString()); int Two=int.Parse (DataGridView1["Safekeep",e.RowIndex].Value.ToString()); if(One<=Two) { DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red; } } //以上在CellFormatting事件里试验一下吧,另外需要注意的是空值null的情况需要单独处理,在此我就不多说了,希望你试验OK 搞定了..之前测试过int.Parse 跟Convert.ToInt32都不行..原来是我数据库的格式问题..谢谢帮助..!!!!! richTextBox 批量读取 高亮格式的文本 指定账户启动windows service 有个list,由几个list组合而成……如何让list1,list2,。。。,listN中的这个也删除掉呢? 关于COM口读写 C#构造函数设计 安装程序制作的问题 如何读取http包中的内容 @@@@@@有谁用C#写过解析xml的函数,请指教(高分)!!!!! 在winform上如何改变一个控件的大小和位置啊??? [在线等待...]如何改变MDI父窗体的客户端的背景颜色。 (高分)急!求教高手,在线等回复 有没有可能把汉字转换成整型?
/// 颜色设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e != null && e.RowIndex > -1)
{
DataGridViewRow dr = this.dataGridView.Rows[e.RowIndex];
int statusCode = Convert.ToInt32(dr.Cells["IntStatusCode"].Value);
if (statusCode == 4)
{
dr.DefaultCellStyle.ForeColor = Color.Gray;
}
if (statusCode == 8)
{
dr.DefaultCellStyle.ForeColor = Color.Gold;
}
if (statusCode == 12 || statusCode == 16)
{
dr.DefaultCellStyle.ForeColor = Color.Brown;
}
if (statusCode == 20)
{
dr.DefaultCellStyle.ForeColor = Color.Purple;
}
if (statusCode == 24 ||statusCode == 28)
{
dr.DefaultCellStyle.ForeColor = Color.Red;
}
}
this.dataGridView.Columns["IntStatusCode"].Visible = false;
}
int y = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Safekeep"].Value);
if (x == y)
{
DataGridView1.DefaultCellStyle.ForeColor = Color.Red;
}
{
foreach(DataGridViewCell cell in DataGridview1.rows[e.RowIndex])
{
cell.Style .ForeColor =color.red//这样行不
}
}
{
int x = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value);
int y = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Safekeep"].Value);
if (x <= y)
{
foreach (DataGridViewCell cell in DataGridView1.Rows[e.RowIndex].Cells)
{
DataGridView1.DefaultCellStyle.ForeColor = Color.Red; }
} }
1.LZ说的是相等,怎么后面就成了x <= y了
2.符合条件的行变色,哪里用得上foreach再说,DataGridView1.DefaultCellStyle.ForeColor = Color.Red;也和foreach没关系啊
if (x <= y)
DataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red; //这句没亲自调试,LZ先试试
您写的这句测试没有反应不知道是不是我那边int格式的问题。。
{
string strOne=DataGridView1["Quantity",e.RowIndex].Value.ToString();
string strTwo=DataGridView1["Safekeep",e.RowIndex].Value.ToString();
if(strOne==strTwo)
{
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red;
}}
//以上在CellFormatting事件里试验一下吧,另外需要注意的是空值null的情况需要单独处理,在此我就不多说了,希望你试验OK
http://blog.csdn.net/zerocold_1986/archive/2008/04/14/2291947.aspx
可以参考下。
我做这个功能用的是CellValueChanged
//很简单,把string转换成int就可以了
private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int One=int.Parse (DataGridView1["Quantity",e.RowIndex].Value.ToString());
int Two=int.Parse (DataGridView1["Safekeep",e.RowIndex].Value.ToString());
if(One<=Two)
{
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red;
} }
//以上在CellFormatting事件里试验一下吧,另外需要注意的是空值null的情况需要单独处理,在此我就不多说了,希望你试验OK