I have a radgrid on a page. On pageload I would like to open the form template of the radgrid in insert mode so that a user can insert without having to click the "Add new record" link. Is this possible? If so, how can it be done? Below is the radgrid code:
<
rad:RadGrid ID="grd" runat="server" AutoGenerateColumns="false" AllowSorting="true" Skin="None"
CssClass="" GridLines="Horizontal" OnNeedDataSource="NeedDataSource" OnItemCommand="ItemCommand"
OnUpdateCommand="UpdateCommand" OnDeleteCommand="DeleteCommand" OnInsertCommand="InsertCommand"
EnableAJAX="true">
<MasterTableView DataKeyNames="ID" CommandItemDisplay="Bottom" >
<HeaderStyle CssClass="" />
<AlternatingItemStyle CssClass="" />
<Columns>
<rad:GridTemplateColumn UniqueName="ID" SortExpression="ID" >
<ItemStyle CssClass="" />
<ItemTemplate>
<%
# %>
</ItemTemplate>
</rad:GridTemplateColumn>
<rad:GridTemplateColumn UniqueName="" HeaderText="" SortExpression="">
<ItemStyle CssClass="" />
<ItemTemplate>
<%
# %>
</ItemTemplate>
</rad:GridTemplateColumn>
<rad:GridTemplateColumn UniqueName=n" >
<ItemStyle CssClass="" />
<ItemTemplate>
<%
# %>
</ItemTemplate>
</rad:GridTemplateColumn>
<rad:GridTemplateColumn UniqueName="" SortExpression="">
<ItemStyle CssClass="" />
<ItemTemplate>
<%
# Eval("")%>
</ItemTemplate>
</rad:GridTemplateColumn>
<rad:GridTemplateColumn UniqueName="">
<ItemStyle CssClass=""/>
<ItemTemplate>
<asp:ImageButton runat="server" ID="btnEdit" CommandName="Edit"/>
<asp:ImageButton runat="server" ID="btnDelete" CommandName="Delete"
OnClientClick="" CommandArgument='<%# Eval("")%>'/>
</ItemTemplate>
</rad:GridTemplateColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<FormTemplate>
<div class="">
<asp:Label runat="server" ID="lblAddEditTitle" ></asp:Label>
</div>
<div class="editRow">
<div class="editTitleColumn">
</div>
<div class="editColumn">
<asp:DropDownList runat="server" ID=""></asp:DropDownList>
</div>
<div class="editTitleColumn">
</div>
<div class="editColumn">
<asp:TextBox runat="server" ID="" CssClass="" Text=''></asp:TextBox>
</div>
</div>
<div class="editRow">
<div class="editTitleColumn">
</div>
<div class="editColumn">
<asp:DropDownList runat="server" ID=""></asp:DropDownList>
</div>
<div class="editTitleColumn">
</div>
<div class="editColumn">
<asp:TextBox runat="server" ID="" TextMode="MultiLine" Rows="3" CssClass=""></asp:TextBox>
</div>
</div>
<div class="row">
<div class="column">
<asp:ImageButton runat="server" ID="btnUpdate"
CommandName='' />
</div>
<div class="column">
<asp:ImageButton runat="server" ID="btnCancel" CommandName="Cancel" />
</div>
</div>
</FormTemplate>
</EditFormSettings>
<CommandItemTemplate>
<div class="commandItem">
<asp:LinkButton runat="server" ID="btnAddEntry" Text="Add New Entry" CommandName="InitInsert"></asp:LinkButton>
</div>
</CommandItemTemplate>
</MasterTableView>
</rad:RadGrid>
Thank you,Jennifer
19 Answers, 1 is accepted
Check out the code snippet given below.
CS:
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
if (!Page.IsPostBack) |
{ |
RadGrid1.MasterTableView.IsItemInserted = true; |
RadGrid1.Rebind(); |
} |
} |
Thanks
Princy.
Thank you very much for your response. The code snippet worked perfectly.
Jennifer
I can see some problems with pagination in this scenario too.
Any quick thoughts or examples?
Thanks!
Tom
My suggestion in this case is to traverse directly all underlying records from the grid source server-side and identify the record of interest by its key value. Some pointers about the approach you can find in this documentation topic:
http://www.telerik.com/help/aspnet-ajax/grdtotalsingridfooters.html (paragraph Displaying totals for all grid pages)
When you find the relevant item in the source, intercept the PreRender event of the grid, switch to the corresponding page (setting the CurrentPageIndex property of the grid), reference the grid row using the FindItemByKeyValue(keyname, keyvalue) method, set the Edit property of the row to true and refresh the grid invoking its Rebind() method.
Best regards,
Stephen
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
If there is a better way to do this, I'd love to know, but for now this works great.
protected void rgBusinesses_PreRender(object sender, EventArgs e) |
{ |
if (Session[sessionNewRecord] != null) |
{ |
Guid businessRefId = new Guid(Session[sessionNewRecord].ToString()); |
Session[sessionNewRecord] = null; |
int pageIndex = 0; |
int rowIndex =0 ; |
for (int i =0; i < ((VList<BusinessFlat>)rgBusinesses.DataSource).Count; i++) |
{ |
if (((VList<BusinessFlat>)rgBusinesses.DataSource)[i].BusinessRefId == businessRefId) |
{ |
rowIndex = i; |
pageIndex = Convert.ToInt32(Decimal.Round((rowIndex / rgBusinesses.PageSize),0)); |
} |
} |
rgBusinesses.CurrentPageIndex = pageIndex; |
rgBusinesses.Rebind(); |
//do this if you just want to select it, such as after inserting on a sorted grid |
rgBusinesses.SelectedIndexes.Add(rgBusinesses.MasterTableView.FindItemByKeyValue("BusinessRefId", businessRefId).ItemIndex); |
//do this if you want to open the row in edit mode |
//rgBusinesses.MasterTableView.FindItemByKeyValue("BusinessRefId", businessRefId).Edit = true; |
//rgBusinesses.Rebind(); |
} |
} |
protected void rgStores_PreRender(object sender, EventArgs e) |
{ |
if (!string.IsNullOrEmpty(rgStores.MasterTableView.FilterExpression)) |
{ |
Session[sessionFilterExpression] = rgStores.MasterTableView.FilterExpression; |
foreach (Object key in filterHashTable.Keys) |
{ |
GridColumn column = rgStores.MasterTableView.GetColumnSafe(key.ToString()); |
Pair filterPair = (Pair)filterHashTable[key]; |
column.CurrentFilterValue = filterPair.Second.ToString(); |
} |
rgStores.Rebind(); |
} |
if (Session[sessionObjectToFind] != null) |
{ |
Guid storeRefId = new Guid(Session[sessionObjectToFind].ToString()); |
Session[sessionObjectToFind] = null; |
int pageIndex = 0; |
int rowIndex = 0; |
for (int i = 0; i < ((VList<StoreFlat>)rgStores.DataSource).Count; i++) |
{ |
if (((VList<StoreFlat>)rgStores.DataSource)[i].StoreRefId == storeRefId) |
{ |
rowIndex = i; |
pageIndex = Convert.ToInt32(Decimal.Truncate((rowIndex / rgStores.PageSize))); |
} |
} |
rgStores.CurrentPageIndex = pageIndex; |
rgStores.Rebind(); |
try |
{ |
rgStores.SelectedIndexes.Add(rgStores.MasterTableView.FindItemByKeyValue("StoreRefId", storeRefId).ItemIndex); |
} |
catch (Exception ex) |
{ |
//this is here for filtering |
//TODO: figure out a good way to new filtered items |
} |
} |
if (Session[GridMessage.SessionDisplayMessage] != null) |
{ |
GridMessage.DisplayMessage(rgStores, (DisplayMessage)Session[GridMessage.SessionDisplayMessage], false, this.Context); |
} |
} |
And then you need sorting logic in your NeedDataSource handler:
protected void rgStores_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
{ |
VList<StoreFlat> stores = new DataRepository.StoreFlatProvider.GetAll(); |
if (rgStores.MasterTableView.SortExpressions.Count == 0) |
{ |
stores.Sort("DivisionName, StoreName"); |
} |
else |
{ |
string sortExpression = string.Empty; |
for (int i = 0; i < rgStores.MasterTableView.SortExpressions.Count; i++) |
{ |
sortExpression += rgStores.MasterTableView.SortExpressions[i].ToString(); |
if (i + 1 < rgStores.MasterTableView.SortExpressions.Count) |
{ |
sortExpression += ","; |
} |
} |
stores.Sort(sortExpression); |
} |
rgStores.DataSource = stores; |
} |
I'm trying to do the same thing as described in the original post.
When I use the suggested code:
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
if (!Page.IsPostBack) |
{ |
RadGrid1.MasterTableView.IsItemInserted = true; |
RadGrid1.Rebind(); |
} |
} |
what I get is an empty editform without any of the columns from the radgrid and inserts can be made. However, the editform does contain the Insert Cancel command in it. This occurs whether I use the popup, in-place, or editform.
The columns for my radgrid are programmatically added.
Has anyone encounter this? What I'm I doing wrong?
thanks in advance,
cathy
Are you using an autogenerated editform or FormTemplate/UserControl editform? If you are using any edit form other than the autogenerated one make sure that you have added enough TextBox controls in the edit form.
Shinu
Thanks for the quick reply.
I'm using an autogenerated editform.
Any suggestions?
thanks,
cathy
Correction to my original post: "what I get is an empty editform without any of the columns from the radgrid and inserts can't be made. However, the editform does contain the Insert Cancel command in it."
If you are using AutoGeneratedEditform it will show TextBox for a GridBoundColumn while in edit/Insert mode. I am not sure what exactly is causing this issue. Can you check whether you have set the ReadOnly property of the Grid columns to true? If so try setting it to false since setting the readonly property will hide the GridColumn from appearing in the Edit/Insert form.
Shinu
Thanks for getting back to me.
I have tried explicitely setting the columns' ReadOnly property to false. It had no effect.
You write that "using AutoGeneratedEditform it will show TextBox for GridBoundColumn while in edit/insert mode.".
Should it also work for other columns types as well? The columns that I programmatically add to the Radgrid are customized columns that extend the GridBoundColumn. Specficially, they implement the google-like filtering funtions.
thanks,
cathy
Yes, it was indeed the setting for ReadOnly property of the Grid columns. They were set else in the program and I didn't catch them.
Thank you very much for your quick replies.
I would still like to confirm that this should work for all columns types even the customized ones that inherits from other columns types?
thanks again,
cathy
As far as I know all the properties set for a Base class will be inherited by the Inherited class. Hence this should work for the inherited column as well.
Thanks
Shinu
cathy
I'm using the same code. For me it doesn't work. I'm also using an ajax UpdatePanel. Can that combination be a problem?
I am using the following code to edit my grid. The Caption Format String lets me add text to the edit form. How do I add similar text to when I am adding (inserting) new record? I mean which attributes need to be set for insert caption?
Thanks
milind
<
EditFormSettings
ColumnNumber
=
"2"
CaptionDataField
=
"service_name"
CaptionFormatString
=
"Edit properties of Item {0}. Changes will be visible to public."
FormCaptionStyle-Wrap
=
"true"
>
<
FormTableItemStyle
Wrap
=
"false"
></
FormTableItemStyle
>
<
FormCaptionStyle
CssClass
=
"EditFormHeader"
></
FormCaptionStyle
>
<
FormMainTableStyle
GridLines
=
"None"
CellSpacing
=
"0"
CellPadding
=
"3"
BackColor
=
"White"
Width
=
"100%"
/>
<
FormTableStyle
CellSpacing
=
"0"
CellPadding
=
"2"
BackColor
=
"ButtonFace"
/>
<
FormTableAlternatingItemStyle
Wrap
=
"False"
></
FormTableAlternatingItemStyle
>
<
EditColumn
ButtonType
=
"ImageButton"
InsertText
=
"Insert Coupon"
UpdateText
=
"Update coupon"
UniqueName
=
"EditCommandColumn1"
CancelText
=
"Cancel edit"
>
</
EditColumn
>
<
FormTableButtonRowStyle
HorizontalAlign
=
"Right"
CssClass
=
"EditFormButtonRow"
></
FormTableButtonRowStyle
>
</
EditFormSettings
>
Yes, the EditFormSettings -> InsertCaption property is what you are looking for. It allows you to specify default insert caption for the newly inserted record. CaptionFormatString is not meaningful for the insert caption.
Best,
Sebastian
the Telerik team
we have different set of controls that open in add and edit mode. so i'd like to be able to open the same record in edit mode after the Insert command is fired.
You can achieve this by putting again the relevant item in edit mode in the Page_PreRender event. In order to easily find the just inserted item you can keep a reference of some of its inserted values and then you can search for it by using them. Note that you also have to rebind the grid after the item is put in edit mode for the changes to take effect.
Here is a sample code snippet illustrating the approach:
string
productName =
""
;
bool
isItemInserted =
false
;
protected
void
RadGrid1_ItemInserted(
object
source, GridInsertedEventArgs e)
{
isItemInserted =
true
;
Hashtable ht =
new
Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(ht, e.Item);
productName = ht[
"ProductName"
].ToString();
}
protected
void
Page_PreRender(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.Items)
{
if
(item[
"ProductName"
].Text==productName && isItemInserted)
{
item.Edit =
true
;
isItemInserted =
false
;
productName =
string
.Empty;
RadGrid1.Rebind();
}
}
}
Kind regards,
Marin
the Telerik team