读书人

DataGridView row的颜色[]

发布时间: 2012-02-21 16:26:23 作者: rapoo

DataGridView row的颜色[在线等]
判定某一个Cell的值
然后把符合条件的Cell所在的Row里所有的Cell的背景色都改成指定的颜色
也就是改变row的背景色

[解决办法]
BACKCOLOR

或者用脚本写ROW的ON》》》事件
[解决办法]
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
How do I color a individual cell depending upon its value or some external method?

[VB.NET}

Public Class DataGridColoredTextBoxColumn

Inherits DataGridTextBoxColumn



Public Sub New()

End Sub



Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)



' the idea is to conditionally set the foreBrush and/or backbrush

' depending upon some crireria on the cell value

' Here, we color anything that begins with a letter higher than 'F '

Try

Dim o As Object

o = Me.GetColumnValueAtRow(source, rowNum)

If (Not (o) Is Nothing) Then

Dim c As Char

c = CType(o, String).Substring(0, 1)

If (c > "F ") Then

' could be as simple as

' backBrush = new SolidBrush(Color.Pink);

' or something fancier...

backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)

foreBrush = New SolidBrush(Color.White)

End If

End If

Catch ex As Exception

' empty catch

Finally

' make sure the base class gets called to do the drawing with

' the possibly changed brushes

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

End Try



End Sub

End Class


[解决办法]
如果是Winform则使用CellFormatting事件处理
[解决办法]
自己改一下
我是用C#写的,现在忙,你自己改一下

/// <summary>
/// 定逾限背景色

/// 用於入完整件行定逾限背景色

/// 定逾限背景色

/// </summary>
private void Setbackclolor()
{
if (dataGridView1.RowCount > 0)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[dataGridView1.Rows.Count - 6].Cells[j].Value.ToString().Trim() == " " || dataGridView1.Rows[dataGridView1.Rows.Count - 6].Cells[j].Value.ToString().Trim() == "-9999 ")
{
}
else
{
double Number = Convert.ToDouble(dataGridView1.Rows[dataGridView1.Rows.Count - 6].Cells[j].Value);
for (int i = 0; i < dataGridView1.Rows.Count - 6; i++)
{
if (dataGridView1.Rows[i].Cells[j].Value.ToString().Trim() != " ")


{
if (Number < Convert.ToDouble(dataGridView1.Rows[i].Cells[j].Value))
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}

}

}
}
}
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Style.BackColor = Color.PaleTurquoise;
dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0].Style.BackColor = Color.PaleTurquoise;
dataGridView1.Rows[dataGridView1.Rows.Count - 3].Cells[0].Style.BackColor = Color.PaleTurquoise;
dataGridView1.Rows[dataGridView1.Rows.Count - 4].Cells[0].Style.BackColor = Color.PaleTurquoise;
dataGridView1.Rows[dataGridView1.Rows.Count - 5].Cells[0].Style.BackColor = Color.PaleTurquoise;
dataGridView1.Rows[dataGridView1.Rows.Count - 6].Cells[0].Style.BackColor = Color.PaleTurquoise;
}
}
/// <summary>
/// 定超背景色
/// 用於入完整件行定超背景色
/// 定超背景色
/// </summary>
private void OverReviseTHSV()
{
string[] dubItem = new string[20];
string strItemchoose = " ";
string strtemp = " ";
try
{
ErmsDB ermsdb = new ErmsDB();
if (this.btnSiteLOV.Enabled == true)
{
dubItem = new string[dtItem.Rows.Count];
for (int i = 0; i < dtItem.Rows.Count; i++)
{
strItemchoose = dtItem.Rows[i][0].ToString();
Sqllimit = string.Format( "SELECT ReviseTHSV FROM T_BaseInfo_GeneralParameter3 where NumberNO = '{0} 'and NO = '{1} ' ", strchoose, strItemchoose);
if (ermsdb.GetTable(Sqllimit).Rows.Count > 0)
{
strtemp = ermsdb.GetTable(Sqllimit).Rows[0][0].ToString();
dubItem[i] = strtemp;
}
else
{
dubItem[i] = "-9999 ";
}
}

}
else
{
dubItem = new string[dtSite.Rows.Count];
for (int i = 0; i < dtSite.Rows.Count; i++)
{
strItemchoose = dtSite.Rows[i][0].ToString();
Sqllimit = string.Format( "SELECT ReviseTHSV FROM T_BaseInfo_GeneralParameter3 where NumberNO = '{0} 'and NO = '{1} ' ", strItemchoose, strchuse);
if (ermsdb.GetTable(Sqllimit).Rows.Count > 0)
{
strtemp = ermsdb.GetTable(Sqllimit).Rows[0][0].ToString();
dubItem[i] = strtemp;
}
else
{
dubItem[i] = "-9999 ";
}
}
}
}
catch (Exception eo)
{
LogEntry logError = new LogEntry();
logError.Message = eo.ToString();
Dictionary <string, object> errorInfo = new Dictionary <string, object> ();


errorInfo.Add( "DateTime ", DateTime.Now);
errorInfo.Add( "Exception Message ", eo.Message);
errorInfo.Add( "Operation ", "WaterDayBrowse--> MakesureQuery ");
logError.ExtendedProperties = errorInfo;
logError.Categories.Add( "Error ");
Logger.Write(logError);
MessageBox.Show(ResourceFactory.GetMultilanguage( "GENERALERRORMESSAGE "));
}
if (dataGridView1.RowCount > 0)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
if (dubItem[j - 1].ToString().Trim() == " " || dubItem[j - 1].ToString().Trim() == "-9999 ")
{
}
else
{
double Number = Convert.ToDouble(dubItem[j - 1]);
for (int i = 0; i < dataGridView1.Rows.Count - 6; i++)
{
if (dataGridView1.Rows[i].Cells[j].Value.ToString().Trim() != " ")
{
if (Number < Convert.ToDouble(dataGridView1.Rows[i].Cells[j].Value))
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Gainsboro;
}

}

}
}
}
}
}

读书人网 >VB Dotnet

热点推荐