9 Replies - 6981 Views - Last Post: 07 March 2011 - 02:09 PM Rate Topic: -----

#1 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

datagridview row's default cell style updates late

Posted 02 March 2011 - 05:58 PM

I have a method that periodically changes the datagridviewrow.defaultcellstyle.backcolor property. I am having two problems with this property. The first is that if i try and change the very first row in the datagrid the color will not change. So with the code below and starting with a rowcount of 0 the first cell will stay the default color of white and all remaining odd cells will change to yellow.

private void AddRow(double val)
{
  datagridview.rowcount++;
  
  if (datagridview.rowcount % 2 == 1)
    datagridview.rows[datagridview.rowcount-1].defaultcellstyle.backcolor = color.yellow;
}



My next problem is that if i set the currentcell of the datagrid on each update, the update will not be displayed until the following update occurs. For some reason setting the currentcell (to ensure the datagrid shows the latest data) causes the backcolor update to be delayed.

private void AddRow(double val)
{
  datagridview.rowcount++;
  
  if (datagridview.rowcount % 2 == 0)
    datagridview.rows[datagridview.rowcount-1].defaultcellstyle.backcolor = color.yellow;

  datagridview.currentcell = datagridview[0, datagridview.rowcount-1];
}



What am i missing here???

Is This A Good Question/Topic? 0
  • +

Replies To: datagridview row's default cell style updates late

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: datagridview row's default cell style updates late

Posted 02 March 2011 - 06:08 PM

Try calling .Refresh() on the control once you do the update.
Was This Post Helpful? 0
  • +
  • -

#3 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: datagridview row's default cell style updates late

Posted 02 March 2011 - 06:10 PM

Sorry, forgot to mention that i have tried both refresh() and update() and neither solve these problems.
Was This Post Helpful? 0
  • +
  • -

#4 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: datagridview row's default cell style updates late

Posted 02 March 2011 - 10:40 PM

A couple of question .

What method of update are u using.

What are you trying to do???


I can only tell you from what you
said that your default cell is yellow
if you remove the selected cell

Do you want the selected cell to change yellow ??

Please supply more info

BTW C# is case sensitive

  if (dataGridView1.RowCount % 2 == 0)
            {
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.Yellow;

            }
            
                    dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.RowCount -1];
                

            
        }

This post has been edited by marinus: 02 March 2011 - 11:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: datagridview row's default cell style updates late

Posted 03 March 2011 - 09:57 AM

View Postmarinus, on 02 March 2011 - 10:40 PM, said:

A couple of question .

What method of update are u using.

What are you trying to do???


Sorry i should always remember that more info is better than not enough. I have a client/server app. Multiple clients will be sending data to the server, the server will verify the info and send updates to the clients every time this data is received. When the client receives the update a new row is added to the data grid with the new data. When a client receives back the data they sent i want to change the backcolor of that row to lightyellow to signify to the client that this was their data.

View Postmarinus, on 02 March 2011 - 10:40 PM, said:

I can only tell you from what you
said that your default cell is yellow
if you remove the selected cell

Do you want the selected cell to change yellow ??


I want 2 things to happen. I want the rows backcolor to change when a certain condition is met and i want the datagrid to autoscroll to the newest data. hence the use of DataGridView1.CurrentCell = blahblah...

Quote

BTW C# is case sensitive


I am aware... i just typed this up quickly to give you an idea of my problem. Its not my actual code but i tested it and it performs the same way.
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: datagridview row's default cell style updates late

Posted 03 March 2011 - 10:34 AM

Bad move! Always post your actual code here. Never type in by memory if you expect people to give helpful suggestions.
Was This Post Helpful? 0
  • +
  • -

#7 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: datagridview row's default cell style updates late

Posted 03 March 2011 - 10:45 AM

View PostSergio Tapia, on 03 March 2011 - 10:34 AM, said:

Bad move! Always post your actual code here. Never type in by memory if you expect people to give helpful suggestions.


Originally i didnt see the need to post the actual code but i have changed my mind. After all, i am the one requesting help. Here is my code.

        public void UpdateControl(double standBid, int currentWinner)
        {
            try
            {
                lblPrice.Text = standBid.ToString();

                CheckReserveMet();

                MinBid = standBid + Increment;

                dgvBidHistory.RowCount++;
                if (currentWinner == ID)
                {
                    dgvBidHistory.Rows[dgvBidHistory.RowCount - 1].DefaultCellStyle.BackColor = Color.LightYellow;
                    dgvBidHistory[0, dgvBidHistory.RowCount - 1].Value = standBid.ToString() + "*";
                    //dgvBidHistory.Update();
                }
                else
                    dgvBidHistory.Rows[dgvBidHistory.RowCount - 1].Cells[0].Value = standBid.ToString();

                dgvBidHistory.CurrentCell = dgvBidHistory[0, dgvBidHistory.RowCount - 1];
            }
            catch (Exception ex)
            {
                EventLog.write("Error UpdateControl: ", ex);
            }
        }


This post has been edited by german129: 03 March 2011 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: datagridview row's default cell style updates late

Posted 03 March 2011 - 12:12 PM

What you can do is loop through every row and cell with
the for loop
if condition(You have to have some valid condition) is met then change color.


for(int x = 0 ; x < dgupdate.Rows.Count; x++)
{
  for(int y = 0 ; y < dgupdate.Rows.Count; y++)
  {
    if(dgupdate.Rows[x].Cells[y] == MetMyLogic)
    {
     //Change Color here ....
    }

  }

}
//Change selected row 

dgupdate.Rows[dgupdate.Rows.Count].Selected = true; 






Select the row you want


How to update the DataGrid , well this should be easy . reload datagrid , the method or where grid starts to get data.




Hope this helps .

This post has been edited by marinus: 03 March 2011 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: datagridview row's default cell style updates late

Posted 03 March 2011 - 12:26 PM

marinus

I am not having a problem changing the backcolor of the cell that i want. the problem is that the change is not reflected in the datagrid until the next time my method is called. for example, if i used your code and replaced "// change color here" with


dgvBidHistory.Rows[dgvBidHistory.RowCount - 1].DefaultCellStyle.BackColor = Color.LightYellow;




the back color of that row will not change to light yellow until the next time this method is run. The datagrid is not redrawing itself or if i force it to redraw using .refresh() or .update() it is not reflecting the change right away. Maybe i need to change the cells default cell style instead? this seems redundant.

for (int i = 0; i < dgvBidHistory.ColumnCount-1; ++i)
{
      dgvBidHistory[i, dgvBidHistory.RowCount - 1].DefaultCellStyle.BackColor = Color.LightYellow;
}


Was This Post Helpful? 0
  • +
  • -

#10 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: datagridview row's default cell style updates late

Posted 07 March 2011 - 02:09 PM

Ok I just realized that I am a moron. All of my testing was with datagrids of only 1 column so when i set the currentcell property it was selecting the only cell in that row and using the SelectionBackColor which was still white. This made it appear as though the color was not being change until the next update but this was just because that cell was unselected in the next period. Stupid mistake.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1