---
title: "Bakgrundsfärg i cell i DataGrid beroende på värde."
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/76296-bakgrundsfärg-i-cell-i-datagrid-beroende-på-värde"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "saw"
published: "2003-05-08T07:43:51.000Z"
updated: "2003-05-08T11:53:59.000Z"
replies: 2
views: 190
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/76296-bakgrundsfärg-i-cell-i-datagrid-beroende-på-värde"
---

# Bakgrundsfärg i cell i DataGrid beroende på värde.

## #1 — saw, 2003-05-08T07:43Z

Lång titel, men ville vara så exact som möjligt.

Önskemål:
Jag fyller i varje dag till en datagrid mina placeringar på mp3.com.
Jag vill att datagriden skall reagera på olika placeringar.

Kod:
Jag har hittat följande kod på nätet, som jag tror är en bra grund att stå på:

```
//SQL  och sd1 är inte med här.
// Create a table style that will hold the new column style 
// that we set and also tie it to our customer's table from our DB
				DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "frånPlaceringar";

// since the dataset has things like field name and number of columns,
// we will use those to create new columnstyles for the columns in our DB table
int numCols = ds1.Tables["frånPlaceringar"].Columns.Count;
			
DataGridColoredTextBoxColumn aColumnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
				
ColumnTextColumn = new DataGridColoredTextBoxColumn();
				
aColumnTextColumn.HeaderText = ds1.Tables
["frånPlaceringar"].Columns[i].ColumnName;

					
aColumnTextColumn.MappingName = ds1.Tables
["frånPlaceringar"].Columns[i].ColumnName;
					
tableStyle.GridColumnStyles.Add(aColumnTextColumn);
				
}			
				
// make the dataGrid use our new tablestyle and bind it to our table
				
dataGrid1.TableStyles.Clear();
				
dataGrid1.TableStyles.Add(tableStyle);
				
dataGrid1.DataSource = ds1.Tables["frånPlaceringar"];

// och nästa

public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
		
{
			
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds,
 System.Windows.Forms.CurrencyManager source, int rowNum,
 System.Drawing.Brush backBrush, System.Drawing.Brush
 foreBrush, bool alignToRight)
			
{
				
// 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
				
{
					
object o = this.GetColumnValueAtRow(source, rowNum);
					
if( o!= null)
					
{
						
char c = ((string)o)[0];
						
if( c < 'F')
						
{
							
// could be as simple as
							
// backBrush = new SolidBrush(Color.Pink);
							
// or something fnacier...
							
backBrush = new LinearGradientBrush(bounds, 
								
Color.FromArgb(255, 200, 200), 
								
Color.FromArgb(128, 20, 20),
								
LinearGradientMode.BackwardDiagonal);
							
foreBrush = new SolidBrush(Color.White);
						
}
					
}
				
}
				
catch(Exception ex){ /* empty catch */ }
				
finally
				
{
					
// make sure the base class gets called to do the drawing with
					
// the possibly changed brushes
					
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
				
}
			
}
		
		
}
```

Problem
Jag kan inte få koden att kolla siffror (har försökt med 
int c = ((string)o)\[0\];
						
if( c == '1')
Men det fungerar inte.
Dessutom så går den tydligen bara på första kolumnen och jag är osäker på vad jag skall "leka med".

Vore väldigt tacksam för förslag.

Permalänk: https://www.webforum.nu/p/76296

## #2 — NETwork, 2003-05-08T09:07Z

Du ska inte ha enkelfnutt ' om du jämför en int, det använder du om du har en character.

```
if(string == "string")
if(int == int)
if(char == 'char')
```

Permalänk: https://www.webforum.nu/p/1040780

## #3 — saw, 2003-05-08T11:53Z

Så enkelt var det.
Tack för hjälpen.

Permalänk: https://www.webforum.nu/p/1040918

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/76296-bakgrundsfärg-i-cell-i-datagrid-beroende-på-värde
