110,538
社区成员
发帖
与我相关
我的任务
分享
DataGridViewRow dgr = dataGridView1.Rows[3]; //假设是第三列成绩,如果是2-5列的成绩呢?
object obj=dgr.Cells[3].Value;
int i;
i=Convert.ToInt32(obj);
if (i < 60)
{
dataGridView1.Rows[3].DefaultCellStyle.ForeColor = Color.Red;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// int eindex = e.RowIndex + 1;
if (e.RowIndex > -1)
{
int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
// MessageBox.Show(intGrade.ToString())
if (intGrade < 60)
{
this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
}
}
}
[/quote]
依此类托 一般如果想要修改控件默认显示方式(颜色、字体、样式等等) 你可以注册一些类似CellPainting的事件 在事件处理程序中 根据条件(一般e参数可以获取想要的东西) 来自定义控件的显示方式
控件内部一般这样实现的:
// ...
DataGridViewCellPaintingEventArgs e = new DataGridViewCellPaintingEventArgs(...);
if(CellPainting != null)
{
CellPainting(this,e); //
}
//接着绘制控件
//...
你要做的:
1.注册类似CellPaiting事件
2.通过e参数判断是否满足条件
3.修改显示方式 private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// int eindex = e.RowIndex + 1;
if (e.RowIndex > -1)
{
int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
// MessageBox.Show(intGrade.ToString())
if (intGrade < 60)
{
this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
}
}
}
//private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
//{
// if (e.ColumnIndex == this.dataGridView1.Columns["courceNetDataGridViewTextBoxColumn"].Index)//根据成绩设置单元格样式
// {
// object obj = this.dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value;
// int n = Convert.ToInt32(obj);//对不及格的成绩设置特殊样式
// if (n < 60)
// {
// DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
// cellStyle.ForeColor = Color.Red;
// //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
// //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
// //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
// }
// }
//}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
if (intGrade < 60)
{
this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.SelectionForeColor = Color.Red;
this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
}
}
//foreach (DataRow row1 in dataGridView1.Rows)
//{
// DataGridViewRow gridRow = new DataGridViewRow();
// DataGridViewButtonCell btn_cell = new DataGridViewButtonCell();
// btn_cell.Value = "通过";
// for (int col_index = 0; col_index < dataGridView1.Columns.Count; col_index++)
// {
// DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
// cell.Value = row1[col_index];
// gridRow.Cells.Add(cell);
// if (col_index == 3)
// {
// if (Convert.ToDecimal(row1[3]) < 60)
// {
// DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
// cellStyle.ForeColor = Color.Red;
// cell.Style = cellStyle;
// btn_cell.Value = "补考";
// }
// }
// }
// gridRow.Cells.Add(btn_cell);
// dataGridView1.Rows.Add(gridRow);
//}
[/quote]
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.Value != null)
{
string s = e.Value.ToString();
if (s == "111")
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
}
}
}
这个没问题 你再自己检查检查
//private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
//{
// if (e.ColumnIndex == this.dataGridView1.Columns["courceNetDataGridViewTextBoxColumn"].Index)//根据成绩设置单元格样式
// {
// object obj = this.dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value;
// int n = Convert.ToInt32(obj);//对不及格的成绩设置特殊样式
// if (n < 60)
// {
// DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
// cellStyle.ForeColor = Color.Red;
// //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
// //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
// //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
// }
// }
//}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["courceNetDataGridViewTextBoxColumn"].Value);
if (intGrade < 60)
{
this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.SelectionForeColor = Color.Red;
this.dataGridView1.Rows[e.RowIndex].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
}
}
//foreach (DataRow row1 in dataGridView1.Rows)
//{
// DataGridViewRow gridRow = new DataGridViewRow();
// DataGridViewButtonCell btn_cell = new DataGridViewButtonCell();
// btn_cell.Value = "通过";
// for (int col_index = 0; col_index < dataGridView1.Columns.Count; col_index++)
// {
// DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
// cell.Value = row1[col_index];
// gridRow.Cells.Add(cell);
// if (col_index == 3)
// {
// if (Convert.ToDecimal(row1[3]) < 60)
// {
// DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
// cellStyle.ForeColor = Color.Red;
// cell.Style = cellStyle;
// btn_cell.Value = "补考";
// }
// }
// }
// gridRow.Cells.Add(btn_cell);
// dataGridView1.Rows.Add(gridRow);
//}