Posted By

pckujawa on 11/19/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


Creating Bindable Objects (Having a DataGridView Show a List of Your Custom Objects)


 / Published in: C#
 

URL: http://windowsclient.net/learn/video.aspx?v=82523

The main thing you need to watch out for is implementing the INotifyPropertyChanged interface in order to force the DGV to update values that were affected by changing other values.

Code taken from the URL.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. //How to: Implement the INotifyPropertyChanged Interface
  10. //http://msdn.microsoft.com/en-us/library/ms229614.aspx
  11.  
  12. namespace HDI_Winforms_BindableObjects
  13. {
  14. public partial class Form1 : Form
  15. {
  16. BindingList<OrderData> ProductList = new BindingList<OrderData>();
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. LoadData();
  21. }
  22.  
  23. private void LoadData()
  24. {
  25. ProductList.Add(new OrderData("Apples", 1, null, null));
  26. ProductList.Add(new OrderData("Oranges", 2, null, null));
  27. ProductList.Add(new OrderData("Bananas", 3, null, null));
  28. ProductList.Add(new OrderData("Pears", 4, null, null));
  29. this.DataGridView1.DataSource = ProductList;
  30. }
  31.  
  32. }
  33.  
  34. public class OrderData : INotifyPropertyChanged
  35. {
  36. public event PropertyChangedEventHandler PropertyChanged;
  37.  
  38. private void NotifyPropertyChanged(String info)
  39. {
  40. if (PropertyChanged != null)
  41. {
  42. PropertyChanged(this, new PropertyChangedEventArgs(info));
  43. }
  44. }
  45. private string _ItemName;
  46. public string ItemName
  47. {
  48. get { return _ItemName; }
  49. set
  50. {
  51. _ItemName = value;
  52. }
  53. }
  54. private decimal _UnitPrice;
  55. public decimal UnitPrice
  56. {
  57. get { return _UnitPrice; }
  58. set
  59. {
  60. _UnitPrice = value;
  61. }
  62. }
  63.  
  64. private int? _Quantity;
  65. public int? Quantity
  66. {
  67. get { return _Quantity; }
  68. set
  69. {
  70. _Quantity = value;
  71. NotifyPropertyChanged("Quantiy");
  72. NotifyPropertyChanged("ExtPrice");
  73. }
  74. }
  75. private DateTime? _OrderDate;
  76. public DateTime? OrderDate
  77. {
  78. get { return _OrderDate; }
  79. set
  80. {
  81. _OrderDate = value;
  82. NotifyPropertyChanged("OrderDate");
  83. NotifyPropertyChanged("ShipDate");
  84. }
  85. }
  86. public DateTime? ShipDate
  87. {
  88. get { return _OrderDate + new TimeSpan(30, 0, 0, 0); }
  89. }
  90.  
  91.  
  92. public decimal? ExtPrice
  93. {
  94. get { return _Quantity * _UnitPrice; }
  95. }
  96. public OrderData(string ItmName, decimal PriceEach, int? Qty, DateTime? OrdDate)
  97. {
  98. _ItemName = ItmName;
  99. _UnitPrice = PriceEach;
  100. _Quantity = Qty;
  101. _OrderDate = OrdDate;
  102. }
  103. }
  104. }

Report this snippet  

You need to login to post a comment.