BoundColumn.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: BoundColumn
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 60%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.ComponentModel;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class BoundColumn : DataGridColumn
  20. {
  21. public static readonly string thisExpr = "!";
  22. private bool boundFieldDesciptionValid;
  23. private string boundField;
  24. private string formatting;
  25. private PropertyDescriptor desc;
  26. public BoundColumn(): base()
  27. {
  28. }
  29. public override void Initialize()
  30. {
  31. base.Initialize();
  32. desc = null;
  33. boundField = DataField;
  34. formatting = DataFormatString;
  35. boundFieldDesciptionValid = false;
  36. }
  37. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
  38. {
  39. base.InitializeCell(cell, columnIndex, itemType);
  40. Control bindCtrl = null;
  41. Control toAdd = null;
  42. switch(itemType)
  43. {
  44. case ListItemType.Item : goto case ListItemType.SelectedItem;
  45. case ListItemType.AlternatingItem
  46. : goto case ListItemType.SelectedItem;
  47. case ListItemType.SelectedItem
  48. : if(DataField.Length != 0)
  49. bindCtrl = cell;
  50. break;
  51. case ListItemType.EditItem
  52. : if(!ReadOnly)
  53. {
  54. TextBox box = new TextBox();
  55. toAdd = box;
  56. if(DataField.Length != 0)
  57. bindCtrl = box;
  58. }
  59. break;
  60. }
  61. if(toAdd != null)
  62. cell.Controls.Add(toAdd);
  63. if(bindCtrl != null)
  64. bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
  65. //throw new NotImplementedException();
  66. }
  67. private void OnDataBindColumn(object sender, EventArgs e)
  68. {
  69. Control senderCtrl = (Control)sender;
  70. DataGridItem item = (DataGridItem)senderCtrl.NamingContainer;
  71. object data = item.DataItem;
  72. if(!boundFieldDesciptionValid)
  73. {
  74. if(boundField != BoundColumn.thisExpr)
  75. {
  76. desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
  77. if(desc == null && !DesignMode)
  78. {
  79. throw new HttpException(
  80. HttpRuntime.FormatResourceString("File_Not_Found",
  81. boundField));
  82. }
  83. boundFieldDesciptionValid = true;
  84. }
  85. }
  86. object value = data;
  87. string val = String.Empty;
  88. if(desc == null && DesignMode)
  89. {
  90. throw new NotImplementedException();
  91. } else
  92. {
  93. if(desc != null)
  94. value = desc.GetValue(data);
  95. val = FormatDataValue(value);
  96. }
  97. if(senderCtrl is TableCell)
  98. {
  99. if(val.Length == 0)
  100. val = "&nbsp;";
  101. ((TableCell)senderCtrl).Text = val;
  102. } else
  103. {
  104. ((TextBox)senderCtrl).Text = val;
  105. }
  106. }
  107. public virtual string DataField
  108. {
  109. get
  110. {
  111. object o = ViewState["DataField"];
  112. if(o != null)
  113. return (string)o;
  114. return String.Empty;
  115. }
  116. set
  117. {
  118. ViewState["DataField"] = value;
  119. OnColumnChanged();
  120. }
  121. }
  122. public virtual string DataFormatString
  123. {
  124. get
  125. {
  126. object o = ViewState["DataFormatString"];
  127. if(o != null)
  128. return (string)o;
  129. return String.Empty;
  130. }
  131. set
  132. {
  133. ViewState["DataFormatString"] = value;
  134. OnColumnChanged();
  135. }
  136. }
  137. public virtual bool ReadOnly
  138. {
  139. get
  140. {
  141. object o = ViewState["ReadOnly"];
  142. if(o != null)
  143. return (bool)o;
  144. return false;
  145. }
  146. set
  147. {
  148. ViewState["ReadOnly"] = value;
  149. }
  150. }
  151. protected virtual string FormatDataValue(Object dataValue)
  152. {
  153. string retVal = String.Empty;
  154. if(dataValue != null)
  155. {
  156. if(formatting.Length == 0)
  157. retVal = dataValue.ToString();
  158. else
  159. retVal = String.Format(formatting, dataValue);
  160. }
  161. return retVal;
  162. }
  163. }
  164. }