2 // System.Web.UI.WebControls.BoundColumn.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.ComponentModel;
31 namespace System.Web.UI.WebControls {
32 public class BoundColumn : DataGridColumn
35 private string data_format_string;
41 public static readonly string thisExpr = "!";
44 [WebSysDescription ("")]
45 [WebCategory ("Misc")]
46 public virtual string DataField
49 return ViewState.GetString ("DataField", String.Empty);
52 ViewState ["DataField"] = value;
57 [WebSysDescription ("")]
58 [WebCategory ("Misc")]
59 public virtual string DataFormatString
62 return ViewState.GetString ("DataFormatString", String.Empty);
65 ViewState ["DataFormatString"] = value;
70 [WebSysDescription ("")]
71 [WebCategory ("Misc")]
72 public virtual bool ReadOnly
75 return ViewState.GetBool ("ReadOnly", false);
78 ViewState ["ReadOnly"] = value;
82 public override void Initialize ()
84 data_format_string = DataFormatString;
87 public override void InitializeCell (TableCell cell, int columnIndex,
88 ListItemType itemType)
90 base.InitializeCell (cell, columnIndex, itemType);
93 case ListItemType.Item:
94 case ListItemType.SelectedItem:
95 case ListItemType.AlternatingItem:
96 cell.DataBinding += new EventHandler (ItemDataBinding);
98 case ListItemType.EditItem:
99 string df = DataField;
100 TextBox tb = new TextBox ();
101 if (df != null && df != "")
102 tb.DataBinding += new EventHandler (ItemDataBinding);
103 cell.Controls.Add (tb);
108 protected virtual string FormatDataValue (object dataValue)
110 if (dataValue == null)
113 if (data_format_string == String.Empty)
114 return dataValue.ToString ();
116 return String.Format (data_format_string, dataValue);
119 string GetValueFromItem (DataGridItem item)
122 if (DataField != thisExpr) {
123 val = DataBinder.Eval (item.DataItem, DataField);
128 string text = FormatDataValue (val);
129 return (text != "" ? text : " ");
132 void ItemDataBinding (object sender, EventArgs e)
134 Control ctrl = (Control) sender;
135 string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
137 TableCell cell = sender as TableCell;
139 TextBox tb = (TextBox) sender;