2
Answers

Changing the backcolor of a single cell in datatable

I'm making Bulls and Cows game in WPF And when the player tries to guess. The result will be displayed in DataTable which is banded to DataGrid and I want to do the next: if the number is bull then the backcolor of a cell which that number is put in will be red, and if the number is cow, it will be blue, so how to do that please?
  1. void filldatagrid()  
  2. {  
  3. table.Columns.Add("N1"typeof(int));  
  4. table.Columns.Add("N2"typeof(int));  
  5. table.Columns.Add("N3"typeof(int));  
  6. table.Columns.Add("N4"typeof(int));  
  7. table.Columns.Add("Cows"typeof(int));  
  8. table.Columns.Add("Bulls"typeof(int));  
  9. showing.ItemsSource = table.DefaultView;  
  10. }  
  11. private void Grid_Loaded(object sender, RoutedEventArgs e)  
  12. {  
  13. filldatagrid();  
  14. }  
  15. private void comparing()  
  16. {  
  17. int a, b, c, d, cows = 0, bulls = 0;  
  18. a = (gussing / 1000) % 10;  
  19. b = (gussing / 100) % 10;  
  20. c = (gussing / 10) % 10;  
  21. d = gussing % 10;  
  22. if (a == number[0] && b == number[1] && c == number[2] && d == number[3])  
  23. {  
  24. UserInput.Visibility = Visibility.Hidden;  
  25. _try.Visibility = Visibility.Hidden;  
  26. win.Visibility = Visibility.Visible;  
  27. thenumber.Visibility = Visibility.Visible;  
  28. thenumber.Content = "The nubmber" + gussing.ToString();  
  29. }  
  30. else  
  31. {  
  32. if (a == number[0])  
  33. {  
  34. bulls++;  
  35. /*what should be written here??*/  
  36. }  
  37. else if (a == number[1] || a == number[2] || a == number[3])  
  38. cows++;  
  39. if (b == number[1])  
  40. bulls++;  
  41. else if (b == number[0] || b == number[2] || b == number[3]) cows++;  
  42. if (c == number[2])  
  43. bulls++;  
  44. else if (c == number[0] || c == number[1] || c == number[3])  
  45. cows++;  
  46. if (d == number[3])  
  47. bulls++;  
  48. else if (c == number[0] || c == number[1] || c == number[2])  
  49. cows++;  
  50. }  
  51. table.Rows.Add(a, b, c, d, cows, bulls);  
  52. }  
Answers (2)