2 Replies - 669 Views - Last Post: 22 March 2011 - 05:38 AM Rate Topic: -----

#1 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Coloring a DataGridView Row, when a number goes below a certain number

Posted 22 March 2011 - 04:09 AM

I have a datagrid full of products, what i am wondering is if there is a way to color a row from that datagrid when its stock level goes below a certain value?

Pseudo-Code For What I Mean

ON Refresh

For Each Row
{
   IF Stock Level is below 2000
   {
      Row Color = Red
   }
}

This post has been edited by insertAlias: 22 March 2011 - 06:56 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Coloring a DataGridView Row, when a number goes below a certain number

#2 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: Coloring a DataGridView Row, when a number goes below a certain number

Posted 22 March 2011 - 05:02 AM

You can set those values in the DataGridViewRow class.
every row in a DataGreidView is an instance of DataGridViewRow;

have a look here and look into the DefaultCellStyle propperty.

with a foreach you can run through the rows of the DataGridView.

foreach(DataGridViewRow dgvRow in DataGridView.Rows)
{
}


Was This Post Helpful? 1
  • +
  • -

#3 Rico Diesel  Icon User is offline

  • D.I.C Head

Reputation: 62
  • View blog
  • Posts: 122
  • Joined: 06-May 10

Re: Coloring a DataGridView Row, when a number goes below a certain number

Posted 22 March 2011 - 05:38 AM

If you want to change the color of one cell it is possible to subscribe to the OnCellPaint event of the DataGridView. For an entire row you can subscribe to the OnRowPrePaint event to manipulate the entire row. This way you don't have to loop through the entire dataset again. (the dataset gets looped through when the grid gets drawn to the screen).

OnCellPaint:
Parse the content of the cell by checking e.Value and when necessary, change the brush color and/or pen color

OnRowPrePaint:
Parse the content of the cell by getting the e.RowIndex and checking the appropiate cell

Hope this helps,


Rico

This post has been edited by Rico Diesel: 22 March 2011 - 05:39 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1