I have a GridTemplateColumn for a radDatePicker,a GridHTMLEditorColumn, and a GridButtonColumn in my grid (see code below). I need to make these columns show up in edit mode but invisible when doing insert. What kind of object I should cast them to in the _ItemCommand event so that I can set the readonly property?
| <telerik:GridTemplateColumn HeaderText="Warrant Date" UniqueName="WarrantDate" | ||||
| Visible="False" DataType="System.DateTime"> | ||||
| <EditItemTemplate> | ||||
| <telerik:RadDatePicker ID="RadDatePicker1" runat="server" Skin="Office2007" allowEmpty="true" | ||||
| Culture="English (United States)" DbSelectedDate='<%# Eval("WarrantDate") %>' > | ||||
| <DateInput ID="DateInput1" LabelCssClass="radLabelCss_Office2007" Skin="Office2007" | ||||
| runat="server"> | ||||
| </DateInput> | ||||
| <Calendar ID="Calendar1" Skin="Office2007" UseColumnHeadersAsSelectors="False" runat="server" | ||||
| UseRowHeadersAsSelectors="False" ViewSelectorText="x" ShowRowHeaders="false"> | ||||
| </Calendar> | ||||
| <DatePopupButton /> | ||||
| </telerik:RadDatePicker> | ||||
| </EditItemTemplate> | ||||
</telerik:GridTemplateColumn>
|
||||
| <telerik:GridHTMLEditorColumn UniqueName="Charge" HeaderText="Charge" DataField="Charge"> | ||||
| </telerik:GridHTMLEditorColumn> |
Thank you!
Ying
9 Answers, 1 is accepted
I hope you are trying to hide the column in the InsertForm and show it in the edit form. If so you may try out the following code snippet to achieve the desired scenario.
CS:
| protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if ((e.Item is GridEditFormInsertItem) && (e.Item.OwnerTableView.IsItemInserted)) |
| { |
| GridEditFormInsertItem insertItem=(GridEditFormInsertItem)e.Item; |
| insertItem["WarrantDate"].Parent.Visible = false; |
| insertItem["ReOpenWarrant"].Parent.Visible = false; |
| insertItem["Charge"].Parent.Visible = false; |
| } |
| } |
Regards
Shinu.
Shinu,
Thank you for your reply. I tried your code but the code never get executed. I have noticed that the following code will hide 'Charge' in both 'Insert' and 'Edit' mode. If only there is a way to know the mode, then I can do it properly.
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| //make Charge and ReOpenWarrant invisible for edit |
| if (e.Item is GridEditableItem && e.Item.IsInEditMode) |
| { |
| GridEditableItem editItem = (GridEditableItem)e.Item; |
| editItem["Charge"].Parent.Visible = false; |
| editItem["ReOpenWarrant"].Parent.Visible = false; |
| } |
| } |
I am not sure as to what EditMode(InPlace/EditForms/PopUp) are you setting for the mastertableview but to check for the mode in ItemDataBound, you can try out the following code:
c#:
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridEditableItem && e.Item.IsInEditMode)// will fire for both edit and insert modes when EditMode="EditForms" |
| { |
| ...... |
| } |
| //Replace GridEditableItem with GridDataInsertItem for EditMode="InPlace" |
| if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted)// will fire only for insert mode when EditMode="EditForms" |
| { |
| ..... |
| } |
| } |
Hope this makes sense..
-Princy.
Thank you so much! Your code is very helpful. Two additional questions:
1. what is the condition for 'fire only for edit when EditMode=EditForms'?
2. Is the condition the same for 'fire for both edit and insert' with EditMode=EditForms vs EditMode=InPlace?
Ying
I hope the following information will be helpful to you.
| Edit mode |
Insert mode |
Browse mode |
|
| InPlace |
GridDataItem |
GridDataInsertItem |
GridDataItem |
| EditForm | GridEditFormItem |
GridEditFormInsertItem |
GridDataItem |
- e.Item is GridEditableItem is true in all modes, no matter whether the EditMode is InPlace or EditForms.
- e.Item.IsInEditMode is used to distinguish whether the item is in edit/insert mode (in all cases)
- e.Item.OwnerTableView.IsItemInserted applies to the tableview and not to specific item
Let us know if you need further details.
Best regards,
Daniel
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Your information is very helpful. As Princy indicated in his post
1. if (e.Item is GridEditableItem && e.Item.IsInEditMode) // will fire for both edit and insert
2. if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted)// will fire only for insert
Is there an explict condition for 'will fire only for edit' ? Or I can safely use 1 but not 2 for this condition?
Depending on the EditMode you could use different approach:
Please note that you could use a single IF operator - I separated the conditions for better readability.
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if (e.Item.IsInEditMode) //this covers edit and insert |
| { |
| if (!(e.Item is GridEditFormInsertItem)) //not in insert mode (EditMode="EditForms") |
| { |
| //do something |
| } |
| } |
| } |
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if (e.Item.IsInEditMode) //this covers edit and insert |
| { |
| if (!(e.Item is GridDataInsertItem)) //not in insert mode (EditMode="InPlace") |
| { |
| //do something |
| } |
| } |
| } |
Regarding your question:
"e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted"
The unwanted effect with this condition is that it will be executed N times if the OwnerTableView (MasterTableView for example) is in Insert mode. In other words - e.Item is GridEditableItem is true for every data item in RadGrid.
Regards,
Daniel
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
I think you covered everything for me :)