Hi,
I have a grid with 3 columns:
Column 1 - an editable GridDateTimeColumn which displays and accepts a date
Column 2 - an editable GridBoundColumn (with GridTextBoxColumnEditor) which displays and accepts a string
Column 3 - a GridEditCommandColumn
In edit mode, I would like to do the following:
Column 1 - to force the user to select the date from the DatePicker only (i.e. to disable the text box)
Column 2 - to restrict the length of the string which can be entered by the user to 60
Can you please show me how to do these?
Can you please also show me how to set the width of the GridTextBoxColumnEditor from the code behind instead of from ASPX?
The web page is available on http://www.gouw.ws/RadGrid
The code is given below:
ASPX
====
<form id="form1" runat="server">
<div>
<telerik:RadScriptManager ID="radScriptManager" runat="server" />
<telerik:RadGrid ID="radGrid" AutoGenerateColumns="false" GridLines="Vertical" OnNeedDataSource="radGrid_NeedDataSource" OnUpdateCommand="radGrid_UpdateCommand" runat="server">
<ClientSettings>
<Scrolling UseStaticHeaders="true" />
</ClientSettings>
<MasterTableView EditMode="InPlace" TableLayout="Fixed">
<Columns>
<telerik:GridDateTimeColumn DataField="Date" PickerType="DatePicker" DataFormatString="{0:dd/MM/yyyy}" DataType="System.DateTime" HeaderText="Date" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="13%" ItemStyle-HorizontalAlign="Left" UniqueName="Date" />
<telerik:GridBoundColumn DataField="Description" ColumnEditorID="gridTextBoxColumnEditor" DataType="System.String" HeaderText="Description" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="77%" ItemStyle-HorizontalAlign="Left" UniqueName="Description" />
<telerik:GridEditCommandColumn ButtonType="LinkButton" HeaderStyle-Width="10%" UniqueName="Edit" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
<telerik:GridTextBoxColumnEditor ID="gridTextBoxColumnEditor" TextBoxStyle-Width="920px" runat="server" />
</div>
</form>
C#
==
public partial class Default : System.Web.UI.Page
{
private List<Datum> _data = null;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
this._data = (List<Datum>)Session["DATA"];
}
else
{
this._data = Datum.Load();
Session["DATA"] = this._data;
}
}
protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
this.radGrid.DataSource = this._data;
}
protected void radGrid_UpdateCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem item = e.Item as GridEditableItem;
((Datum)(this._data[item.ItemIndex])).Date = ((RadDatePicker)item["Date"].Controls[0]).SelectedDate.Value.Date;
((Datum)(this._data[item.ItemIndex])).Description = ((TextBox)item["Description"].Controls[0]).Text;
}
}
}
public class Datum
{
public DateTime Date { get; set; }
public string Description { get; set; }
public Datum(DateTime date, string description)
{
Date = date;
Description = description;
}
public static List<Datum> Load()
{
List<Datum> data = new List<Datum>();
data.Add(new Datum(DateTime.Now, "This date is today"));
data.Add(new Datum(DateTime.Now.AddDays(1), "This date is tomorrow"));
data.Add(new Datum(DateTime.Now.AddDays(2), "This date is the day after tomorrow"));
return data;
}
}
Thanks & regards,
Herman Gouw
Skype: hermangouw