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 using System.ComponentModel;
17 namespace System.Web.UI.WebControls
19 public class BoundColumn : DataGridColumn
21 public static readonly string thisExpr = "!";
23 private bool boundFieldDescriptionValid;
24 private string boundField;
25 private string formatting;
27 private PropertyDescriptor desc;
29 public BoundColumn(): base()
33 public override void Initialize()
38 boundField = DataField;
39 formatting = DataFormatString;
40 boundFieldDescriptionValid = false;
43 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
45 base.InitializeCell(cell, columnIndex, itemType);
47 Control bindCtrl = null;
51 case ListItemType.Item : goto case ListItemType.SelectedItem;
52 case ListItemType.AlternatingItem
53 : goto case ListItemType.SelectedItem;
54 case ListItemType.SelectedItem
55 : if(DataField.Length != 0)
58 case ListItemType.EditItem
61 TextBox box = new TextBox();
63 if(DataField.Length != 0)
66 goto case ListItemType.SelectedItem;
71 cell.Controls.Add(toAdd);
73 bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
74 //throw new NotImplementedException();
77 private void OnDataBindColumn(object sender, EventArgs e)
79 Control senderCtrl = (Control)sender;
80 DataGridItem item = (DataGridItem)senderCtrl.NamingContainer;
81 object data = item.DataItem;
83 if(!boundFieldDescriptionValid)
85 if(boundField != BoundColumn.thisExpr)
87 desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
88 if(desc == null && !DesignMode)
90 throw new HttpException(
91 HttpRuntime.FormatResourceString("File_Not_Found",
94 boundFieldDescriptionValid = true;
98 string val = String.Empty;
99 if(desc == null && DesignMode)
101 throw new NotImplementedException();
105 value = desc.GetValue(data);
106 val = FormatDataValue(value);
108 if(senderCtrl is TableCell)
112 ((TableCell)senderCtrl).Text = val;
115 ((TextBox)senderCtrl).Text = val;
119 [DefaultValue (""), WebCategory ("Misc")]
120 [WebSysDescription ("The field that this column is bound to.")]
121 public virtual string DataField
125 object o = ViewState["DataField"];
132 ViewState["DataField"] = value;
137 [DefaultValue (""), WebCategory ("Misc")]
138 [WebSysDescription ("A format string that is applied to the data value.")]
139 public virtual string DataFormatString
143 object o = ViewState["DataFormatString"];
150 ViewState["DataFormatString"] = value;
155 [DefaultValue (false), WebCategory ("Misc")]
156 [WebSysDescription ("Determines if the databound field can only be displayed or also edited.")]
157 public virtual bool ReadOnly
161 object o = ViewState["ReadOnly"];
168 ViewState["ReadOnly"] = value;
172 protected virtual string FormatDataValue(Object dataValue)
174 string retVal = String.Empty;
175 if(dataValue != null)
177 if(formatting.Length == 0)
178 retVal = dataValue.ToString();
180 retVal = String.Format(formatting, dataValue);