2 // System.Web.UI.WebControls.BoundColumn.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
38 namespace System.Web.UI.WebControls
40 public class BoundColumn : DataGridColumn
42 public static readonly string thisExpr = "!";
44 private bool boundFieldDescriptionValid;
45 private string boundField;
46 private string formatting;
48 private PropertyDescriptor desc;
50 public BoundColumn(): base()
54 public override void Initialize()
59 boundField = DataField;
60 formatting = DataFormatString;
61 boundFieldDescriptionValid = false;
64 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
66 base.InitializeCell(cell, columnIndex, itemType);
68 Control bindCtrl = null;
72 case ListItemType.Item : goto case ListItemType.SelectedItem;
73 case ListItemType.AlternatingItem
74 : goto case ListItemType.SelectedItem;
75 case ListItemType.SelectedItem
76 : if(DataField.Length != 0)
79 case ListItemType.EditItem
82 TextBox box = new TextBox();
84 if(DataField.Length != 0)
87 goto case ListItemType.SelectedItem;
92 cell.Controls.Add(toAdd);
94 bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
95 //throw new NotImplementedException();
98 private void OnDataBindColumn(object sender, EventArgs e)
100 Control senderCtrl = (Control)sender;
101 DataGridItem item = (DataGridItem)senderCtrl.NamingContainer;
102 object data = item.DataItem;
104 if(!boundFieldDescriptionValid)
106 if(boundField != BoundColumn.thisExpr)
108 desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
109 if(desc == null && !DesignMode)
111 throw new HttpException(
112 HttpRuntime.FormatResourceString("File_Not_Found",
115 boundFieldDescriptionValid = true;
119 string val = String.Empty;
120 if(desc == null && DesignMode)
122 throw new NotImplementedException();
126 value = desc.GetValue(data);
127 val = FormatDataValue(value);
129 if(senderCtrl is TableCell)
133 ((TableCell)senderCtrl).Text = val;
136 ((TextBox)senderCtrl).Text = val;
140 [DefaultValue (""), WebCategory ("Misc")]
141 [WebSysDescription ("The field that this column is bound to.")]
142 public virtual string DataField
146 object o = ViewState["DataField"];
153 ViewState["DataField"] = value;
158 [DefaultValue (""), WebCategory ("Misc")]
159 [WebSysDescription ("A format string that is applied to the data value.")]
160 public virtual string DataFormatString
164 object o = ViewState["DataFormatString"];
171 ViewState["DataFormatString"] = value;
176 [DefaultValue (false), WebCategory ("Misc")]
177 [WebSysDescription ("Determines if the databound field can only be displayed or also edited.")]
178 public virtual bool ReadOnly
182 object o = ViewState["ReadOnly"];
189 ViewState["ReadOnly"] = value;
193 protected virtual string FormatDataValue(Object dataValue)
195 string retVal = String.Empty;
196 if(dataValue != null)
198 if(formatting.Length == 0)
199 retVal = dataValue.ToString();
201 retVal = String.Format(formatting, dataValue);