Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background color for DataGrid

Status
Not open for further replies.

dpdg

Programmer
May 23, 2005
148
US
I have a datagrid in my windows app that I need to set the background of certain rows that are flagged when the datagrid loads.

To clarify a little better: there are certain rows (not alternative rows) that I want to display with a different background color so that the user will know that those rows have had corrections made to them.

How do I set the background color for these rows when the datagrid is loaded?
 
so you what a specific color based on the data in the grid as opposed to an "ever other row"?

-The answer to your problem may not be the answer to your question.
 
Exactly. I'm saying that I do not refer to the alternating color.

I want the background of specific rows to be yellow, for example, so that the user can readily see that the row has been modified (incorrect data corrected) and it is ready to be resubmitted for processing.

This could be set as the Grid is being loaded.
 
you may have trouble with this.

Could you have 2 Grids, one with items for Resubmit and one for the other stuff?

-The answer to your problem may not be the answer to your question.
 
That may be a good idea. I could use the same grid only use a filter to display the rows that have been corrected. That would work for them I think. I'll have to ask my client if he will accept that solution.

I'd still like to see an implementation of the background colors though. That's what the specification calls for.
 
this is easy.

set the backcolor of the grid row in the OnDataBinding event. Check for your condition of a correction - if true, backcolor = whatever.
 
I thought of that, but there is no OnDataBinding event. Isn't that an event in the DataGrid of asp.net?

If I'm wrong, could you tell me where to find it?
 
Code:
private void OnCellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex ==
m_CustomersGrid.Columns["Contact"].Index)
{
e.FormattingApplied = true;
DataGridViewRow row =
m_CustomersGrid.Rows[e.RowIndex];
e.Value = string.Format("{0} : {1}",
row.Cells["ContactName"].Value,
row.Cells["Phone"].Value);
}
}

there probably is OnRowFormatting... just assuming right now, didnt check for sure.
 
OK GridView is a different animal. I'm using Winforms DataGrid in Framework 1.1
 
there are the same type of events for a DataGrid... google them
 

Hello,

Another solution but expensive is to use infragistics tools. There they have a datagrid called the ultragrid, i have used it many times. There you can do exactly what you are asking.

Based on certain conditions you can change the colour of the rows. If you get this tool the code to do this is below:

Code:
For i = 0 To rowCount - 1
            If (Me.UltraGrid1.Rows(i).Cells("Priority").Text = "Urgent") Then
                Me.UltraGrid1.Rows(i).Appearance.BackColor = Color.Red
            ElseIf (Me.UltraGrid1.Rows(i).Cells("Priority").Text = "Normal") Then
                Me.UltraGrid1.Rows(i).Appearance.BackColor = Color.LightGreen
            ElseIf (Me.UltraGrid1.Rows(i).Cells("Priority").Text = "Low") Then
                Me.UltraGrid1.Rows(i).Appearance.BackColor = Color.LightSkyBlue
            End If
        Next i

Hope this helps,

Steve
 
If you don't want to purchase a grid control, you can inherit the standard DataGridTextBoxColumn and do your own custom painting. This code assumes you have a boolean column in the source datatable to show if a row should be highlighted:
Code:
Public Class DataGridHighlightedTextBoxColumn
    Inherits DataGridTextBoxColumn

    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
        
        Dim dr As DataRow = CType(source.List().Item(rowNum), DataRowView).Row

        If Convert.ToBoolean(dr("Highlighted")) Then

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

        Else

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

        End If

    End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top