webForumDet fria alternativet

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

.NET

2 svar · 181 visningar · startad av saw

Medlem sedan mars 20011 880 inlägg
Frågan#1

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.

Medlem sedan juni 20011 732 inlägg
#2

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')
Medlem sedan mars 20011 880 inlägg
#3

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

259 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
128 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
128 ms — ändringar (db)