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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView - Changing the BackColor property 1

Status
Not open for further replies.

andegre

MIS
Oct 20, 2005
275
US
I want to go through and change the backcolor for rows that are equal. I'm doing just a loop comparing the rows (not worried about performance), and when I find duplicates, I've tried to execute all 3 versions of this with no results:

Code:
                        //dgv[cn, oCounter].Style.BackColor = System.Drawing.Color.Red;
                        //dgv[cn, iCounter].Style.BackColor = System.Drawing.Color.Red;
                        //((DataGridViewCell)dgv[cn, oCounter]).Style.BackColor = System.Drawing.Color.Red;
                        //((DataGridViewCell)dgv[cn, iCounter]).Style.BackColor = System.Drawing.Color.Red;
                        ((DataGridViewRow)dgv.Rows[oCounter]).DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                        ((DataGridViewRow)dgv.Rows[iCounter]).DefaultCellStyle.BackColor = System.Drawing.Color.Red;

What other properties do I need to change in order for my colors to be reflected?
 
does this work?
Code:
dgView.Rows[i].Cells["MyColumnName"].Style.BackColor = System.Drawing.Color.Red

Change MyColumnName to your col name.

Age is a consequence of experience
 
Can you try something like this
Code:
foreach (GridViewRow grv in gvSource.Rows)
{
	grv.BackColor = System.Drawing.Color.Red;
}

RalphTrent
 
Can you post your loop as that should work. You may have an event that is setting it back again?

Age is a consequence of experience
 
Code:
const string cn = "LocationStation";

for (int oCounter = 0; oCounter < dgv.Rows.Count; oCounter++)
{
    for (int iCounter = oCounter + 1; iCounter < dgv.Rows.Count; iCounter++)
    {
        if (iCounter == oCounter)
            continue;

        if (dgv[cn, oCounter].Value.ToString() == dgv[cn, iCounter].Value.ToString())
        {
            //dgv[cn, oCounter].Style.BackColor = System.Drawing.Color.Red;
            //dgv[cn, iCounter].Style.BackColor = System.Drawing.Color.Red;
            //((DataGridViewCell)dgv[cn, oCounter]).Style.BackColor = System.Drawing.Color.Red;
            //((DataGridViewCell)dgv[cn, iCounter]).Style.BackColor = System.Drawing.Color.Red;
            //((DataGridViewRow)dgv.Rows[oCounter]).DefaultCellStyle.BackColor = System.Drawing.Color.Red;
            //((DataGridViewRow)dgv.Rows[iCounter]).DefaultCellStyle.BackColor = System.Drawing.Color.Red;
            //dgv.Rows[oCounter].Cells[cn].Style.BackColor = System.Drawing.Color.Red;
            //dgv.Rows[iCounter].Cells[cn].Style.BackColor = System.Drawing.Color.Red;
        }
    }
}

This is everything in the method, and the only other "thing" that fires after this is the Show() method. This all occurs when the form loads.
 
Sorry, gave you the code to change a cell :(

Should be . . .
Code:
dgv.Rows[oCounter].DefaultCellStyle.BackColor = Color.Red;

Any better?

Age is a consequence of experience
 
Still no. I set up my breakpoints on that line and I KNOW it hits it, but it's just never reflected in the GUI.

I still get the feeling it has something to do with the properties that I have setup on the DataGridView. I'll post those in case someone sees something that is contradictory.

Code:
this.dgv.AllowUserToAddRows = false;
this.dgv.AllowUserToDeleteRows = false;
this.dgv.AllowUserToOrderColumns = true;
this.dgv.AllowUserToResizeRows = false;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgv.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dgv.DefaultCellStyle = dataGridViewCellStyle2;
this.dgv.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgv.Location = new System.Drawing.Point(0, 0);
this.dgv.MultiSelect = false;
this.dgv.Name = "dgv";
this.dgv.ReadOnly = true;
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgv.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
this.dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dgv.Size = new System.Drawing.Size(707, 365);
this.dgv.TabIndex = 0;
 
Hmmm. There is a lot of settings here. Give me 20 mins and i will test them.

Age is a consequence of experience
 
This works for me.
Code:
dgv.Rows[oCounter].DefaultCellStyle.BackColor = Color.Red;

Can you try moving the loop to the form Shown event. That way we will know that it is done after the grid is shown

Age is a consequence of experience
 
Can you try moving the loop to the form Shown event. That way we will know that it is done after the grid is shown

Woala! That did the trick. Thank you so much, litton!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top