如数值小于10,所在行显示为红色?
14 个解决方案
#1
在绑定事件中处理
很久没用Winform。
贴一段MSDN的代码
该处理程序根据单元格的列和值更改单元格的显示方式。
public class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
public Form1()
{
// Initialize the images.
try
{
highPriImage = new Bitmap("highPri.bmp");
mediumPriImage = new Bitmap("mediumPri.bmp");
lowPriImage = new Bitmap("lowPri.bmp");
}
catch (ArgumentException)
{
MessageBox.Show("The Priority column requires Bitmap images " +
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
"residing in the same directory as the executable file.");
}
// Initialize the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.AddRange(
new DataGridViewTextBoxColumn(),
new DataGridViewImageColumn());
dataGridView1.Columns[0].Name = "Balance";
dataGridView1.Columns[1].Name = "Priority";
dataGridView1.Rows.Add("-100", "high");
dataGridView1.Rows.Add("0", "medium");
dataGridView1.Rows.Add("100", "low");
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
this.Controls.Add(dataGridView1);
}
// Changes how cells are displayed depending on their columns and values.
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText = stringValue;
// Replace the string value with the image value.
switch (stringValue)
{
case "high":
e.Value = highPriImage;
break;
case "medium":
e.Value = mediumPriImage;
break;
case "low":
e.Value = lowPriImage;
break;
}
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
很久没用Winform。
贴一段MSDN的代码
该处理程序根据单元格的列和值更改单元格的显示方式。
public class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
public Form1()
{
// Initialize the images.
try
{
highPriImage = new Bitmap("highPri.bmp");
mediumPriImage = new Bitmap("mediumPri.bmp");
lowPriImage = new Bitmap("lowPri.bmp");
}
catch (ArgumentException)
{
MessageBox.Show("The Priority column requires Bitmap images " +
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
"residing in the same directory as the executable file.");
}
// Initialize the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.AddRange(
new DataGridViewTextBoxColumn(),
new DataGridViewImageColumn());
dataGridView1.Columns[0].Name = "Balance";
dataGridView1.Columns[1].Name = "Priority";
dataGridView1.Rows.Add("-100", "high");
dataGridView1.Rows.Add("0", "medium");
dataGridView1.Rows.Add("100", "low");
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
this.Controls.Add(dataGridView1);
}
// Changes how cells are displayed depending on their columns and values.
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText = stringValue;
// Replace the string value with the image value.
switch (stringValue)
{
case "high":
e.Value = highPriImage;
break;
case "medium":
e.Value = mediumPriImage;
break;
case "low":
e.Value = lowPriImage;
break;
}
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
#2
晕 这么麻烦??有没有相应的属性什么的??
#3
表名 stockinfo
字段 number
查询表stockinfo,字段number的值小于10的行显示颜色为红色
怎么编写代码啊???
字段 number
查询表stockinfo,字段number的值小于10的行显示颜色为红色
怎么编写代码啊???
#4
for(int i=0;i<ds.Tables["stockinfo"].Rows.Count;i++)
{
if(Convert.ToInt32(ds.Tables["stockinfo"].Row[i]["number"].ToString())<10)
{
this.datagrid1.Rows[i].BackColor=Color.Red;
}
}
#5
private void dgvBankList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == this.dgvBankList.Columns["列名"].Index) && e.Value != null)
{
DataGridViewCell cell = this.dgvBankList.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (int.Parse(dgvBankList.Rows[e.RowIndex].Cells["列名"].Value)>10)
{
cell.ToolTipText = "双击打开其官方网站";
cell.Style.ForeColor = Color.Red;
}
}
}
{
if ((e.ColumnIndex == this.dgvBankList.Columns["列名"].Index) && e.Value != null)
{
DataGridViewCell cell = this.dgvBankList.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (int.Parse(dgvBankList.Rows[e.RowIndex].Cells["列名"].Value)>10)
{
cell.ToolTipText = "双击打开其官方网站";
cell.Style.ForeColor = Color.Red;
}
}
}
#6
drsnipper 的我能看懂
renjnet 的什么意思 我看不明白
renjnet 的什么意思 我看不明白
#7
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["列"].DefaultView[i];
string s = Convert.ToString(mydrv["列"]);
if (Convert.ToDouble(s) < 10) {
GridView1.Rows[i].Cells[列].BackColor = System.Drawing.Color.Red;
}
}
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["列"].DefaultView[i];
string s = Convert.ToString(mydrv["列"]);
if (Convert.ToDouble(s) < 10) {
GridView1.Rows[i].Cells[列].BackColor = System.Drawing.Color.Red;
}
}
#8
GridView1.Rows[i].Cells[“”].BackColor = System.Drawing.Color.Red
#9
如果是webform就使用js控制速度快点!
#10
如果是webform就使用js控制速度快点!
#11
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int v = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
if(v == 1)
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}
#12
第一种方法:使用一个私有方法解决
第二种方法:使用DataGridView的单元格格式化事件
以上两种方法均可。。。
但第二种方法,数据多时感觉非常麻烦!每次窗口重绘都会执行,哪怕是移动一下窗体。
for(int i=0;i<ds.Tables["stockinfo"].Rows.Count;i++)
{
if(Convert.ToInt32(ds.Tables["stockinfo"].Row[i]["number"].ToString())<10)
{
this.datagrid1.Rows[i].BackColor=Color.Red;
}
}
第二种方法:使用DataGridView的单元格格式化事件
private void dgvBankList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgvBankList.Columns[e.ColumnIndex].Name.Equals("列名"))
{
try
{
// e.Value这个属性,就是获得或者设置Cell单元格显示内容的
string str= e.Value.ToString();
switch (str)
{
case "0": e.Value = "已经加入"; break;
case "1": e.Value = "已经删除"; break;
default:
e.Value = "还未加入";
break;
}
//if (i > 10)//如果i〉10,则将该行背景色换成蓝色
//{
// int r = e.RowIndex;
// DataGridViewRow row = dataGridView1.Rows[r];
// row.DefaultCellStyle.BackColor = Color.Blue;
//}
}
catch { }
}
}
以上两种方法均可。。。
但第二种方法,数据多时感觉非常麻烦!每次窗口重绘都会执行,哪怕是移动一下窗体。
#13
顶一下
#14
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int v = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
if(v == 1)
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}
{
int v = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
if(v == 1)
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}
#1
在绑定事件中处理
很久没用Winform。
贴一段MSDN的代码
该处理程序根据单元格的列和值更改单元格的显示方式。
public class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
public Form1()
{
// Initialize the images.
try
{
highPriImage = new Bitmap("highPri.bmp");
mediumPriImage = new Bitmap("mediumPri.bmp");
lowPriImage = new Bitmap("lowPri.bmp");
}
catch (ArgumentException)
{
MessageBox.Show("The Priority column requires Bitmap images " +
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
"residing in the same directory as the executable file.");
}
// Initialize the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.AddRange(
new DataGridViewTextBoxColumn(),
new DataGridViewImageColumn());
dataGridView1.Columns[0].Name = "Balance";
dataGridView1.Columns[1].Name = "Priority";
dataGridView1.Rows.Add("-100", "high");
dataGridView1.Rows.Add("0", "medium");
dataGridView1.Rows.Add("100", "low");
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
this.Controls.Add(dataGridView1);
}
// Changes how cells are displayed depending on their columns and values.
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText = stringValue;
// Replace the string value with the image value.
switch (stringValue)
{
case "high":
e.Value = highPriImage;
break;
case "medium":
e.Value = mediumPriImage;
break;
case "low":
e.Value = lowPriImage;
break;
}
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
很久没用Winform。
贴一段MSDN的代码
该处理程序根据单元格的列和值更改单元格的显示方式。
public class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
public Form1()
{
// Initialize the images.
try
{
highPriImage = new Bitmap("highPri.bmp");
mediumPriImage = new Bitmap("mediumPri.bmp");
lowPriImage = new Bitmap("lowPri.bmp");
}
catch (ArgumentException)
{
MessageBox.Show("The Priority column requires Bitmap images " +
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
"residing in the same directory as the executable file.");
}
// Initialize the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.AddRange(
new DataGridViewTextBoxColumn(),
new DataGridViewImageColumn());
dataGridView1.Columns[0].Name = "Balance";
dataGridView1.Columns[1].Name = "Priority";
dataGridView1.Rows.Add("-100", "high");
dataGridView1.Rows.Add("0", "medium");
dataGridView1.Rows.Add("100", "low");
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
this.Controls.Add(dataGridView1);
}
// Changes how cells are displayed depending on their columns and values.
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText = stringValue;
// Replace the string value with the image value.
switch (stringValue)
{
case "high":
e.Value = highPriImage;
break;
case "medium":
e.Value = mediumPriImage;
break;
case "low":
e.Value = lowPriImage;
break;
}
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
#2
晕 这么麻烦??有没有相应的属性什么的??
#3
表名 stockinfo
字段 number
查询表stockinfo,字段number的值小于10的行显示颜色为红色
怎么编写代码啊???
字段 number
查询表stockinfo,字段number的值小于10的行显示颜色为红色
怎么编写代码啊???
#4
for(int i=0;i<ds.Tables["stockinfo"].Rows.Count;i++)
{
if(Convert.ToInt32(ds.Tables["stockinfo"].Row[i]["number"].ToString())<10)
{
this.datagrid1.Rows[i].BackColor=Color.Red;
}
}
#5
private void dgvBankList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == this.dgvBankList.Columns["列名"].Index) && e.Value != null)
{
DataGridViewCell cell = this.dgvBankList.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (int.Parse(dgvBankList.Rows[e.RowIndex].Cells["列名"].Value)>10)
{
cell.ToolTipText = "双击打开其官方网站";
cell.Style.ForeColor = Color.Red;
}
}
}
{
if ((e.ColumnIndex == this.dgvBankList.Columns["列名"].Index) && e.Value != null)
{
DataGridViewCell cell = this.dgvBankList.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (int.Parse(dgvBankList.Rows[e.RowIndex].Cells["列名"].Value)>10)
{
cell.ToolTipText = "双击打开其官方网站";
cell.Style.ForeColor = Color.Red;
}
}
}
#6
drsnipper 的我能看懂
renjnet 的什么意思 我看不明白
renjnet 的什么意思 我看不明白
#7
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["列"].DefaultView[i];
string s = Convert.ToString(mydrv["列"]);
if (Convert.ToDouble(s) < 10) {
GridView1.Rows[i].Cells[列].BackColor = System.Drawing.Color.Red;
}
}
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["列"].DefaultView[i];
string s = Convert.ToString(mydrv["列"]);
if (Convert.ToDouble(s) < 10) {
GridView1.Rows[i].Cells[列].BackColor = System.Drawing.Color.Red;
}
}
#8
GridView1.Rows[i].Cells[“”].BackColor = System.Drawing.Color.Red
#9
如果是webform就使用js控制速度快点!
#10
如果是webform就使用js控制速度快点!
#11
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int v = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
if(v == 1)
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}
#12
第一种方法:使用一个私有方法解决
第二种方法:使用DataGridView的单元格格式化事件
以上两种方法均可。。。
但第二种方法,数据多时感觉非常麻烦!每次窗口重绘都会执行,哪怕是移动一下窗体。
for(int i=0;i<ds.Tables["stockinfo"].Rows.Count;i++)
{
if(Convert.ToInt32(ds.Tables["stockinfo"].Row[i]["number"].ToString())<10)
{
this.datagrid1.Rows[i].BackColor=Color.Red;
}
}
第二种方法:使用DataGridView的单元格格式化事件
private void dgvBankList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgvBankList.Columns[e.ColumnIndex].Name.Equals("列名"))
{
try
{
// e.Value这个属性,就是获得或者设置Cell单元格显示内容的
string str= e.Value.ToString();
switch (str)
{
case "0": e.Value = "已经加入"; break;
case "1": e.Value = "已经删除"; break;
default:
e.Value = "还未加入";
break;
}
//if (i > 10)//如果i〉10,则将该行背景色换成蓝色
//{
// int r = e.RowIndex;
// DataGridViewRow row = dataGridView1.Rows[r];
// row.DefaultCellStyle.BackColor = Color.Blue;
//}
}
catch { }
}
}
以上两种方法均可。。。
但第二种方法,数据多时感觉非常麻烦!每次窗口重绘都会执行,哪怕是移动一下窗体。
#13
顶一下
#14
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int v = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
if(v == 1)
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}
{
int v = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
if(v == 1)
dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.Red;
}