This is a migrated thread and some comments may be shown as answers.

[Solved] dynamic insert and edit form in radgrid

3 Answers 301 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Duy
Top achievements
Rank 1
Duy asked on 26 Aug 2013, 10:36 PM
declare grid
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="txstate" EnableEmbeddedSkins="false" Width="600px" Visible="false" >
                               <MasterTableView Width="100%" DataKeyNames="ID" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New"
                                   CommandItemSettings-ShowAddNewRecordButton="true" CommandItemSettings-ShowRefreshButton="false">
                                   <PagerStyle Mode="NextPrevAndNumeric" />
                                   <Columns>
                                       <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="LinkButton" EditText="Edit" />
                                       <telerik:GridButtonColumn UniqueName="InsertColumn" CommandName="Add" Text="Add" ButtonType="LinkButton" />
                                       <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName= "DeleteColumn" />
                                   </Columns>
                                   <EditFormSettings EditFormType="Template">
                                       <FormTemplate>
                                           <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
                                       </FormTemplate>
                                   </EditFormSettings>
                               </MasterTableView>
                           </telerik:RadGrid>

create edit form in itemreated event and insert dynamic control to placeholder1 in editform
Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemCreated
                If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then
                    Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
                    Dim PlaceHolder1 As PlaceHolder = DirectCast(editItem.FindControl("PlaceHolder1"), PlaceHolder)
                    PlaceHolder1.Controls.Add(New LiteralControl("<table border='0' cellpadding='5' cellspacing='0' width='100%' align='center'><tr><td width='110px'>"))
                  Dim lb_Description As Label = New Label
                            lb_Description.Text = "Description: "
                            lb_Description.ID = "lb_Description"
                            lb_Description.CssClass = "label"
                            PlaceHolder1.Controls.Add(lb_Description)
                            Dim txt_Description As RadTextBox = New RadTextBox
                            txt_Description.Text = editItem.Item("Description").Text
                            txt_Description.ID = "txt_Description"
                            txt_Description.Width = "200"
                            PlaceHolder1.Controls.Add(txt_Description)

    Dim btn_update As Button = New Button
                    btn_update.Text = "Update"
                    btn_update.ID = "btn_Update"
                    btn_update.CommandName = "Update"
                    btn_update.CssClass = "button"
                    PlaceHolder1.Controls.Add(btn_update)
                    Dim btn_cancel As Button = New Button
                    btn_cancel.Text = "Cancel"
                    btn_cancel.ID = "btn_cancel"
                    btn_cancel.CommandName = "Cancel"
                    btn_cancel.CssClass = "button"
                    PlaceHolder1.Controls.Add(btn_cancel)
End If
End Sub

works great for editting. Now how do i allow this to be able to click on the "Add New Recod" at the command tool bar to access the form to the insert command? Can somebody help me with this please? Thank you and really appreciate it.

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Aug 2013, 04:19 AM
Hi Duy.

I guess you want to have the button to function according to insert and edit mode.Please try the below code snippet.

VB:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
    If (TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode) Then
        Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
        Dim PlaceHolder1 As PlaceHolder = DirectCast(editItem.FindControl("PlaceHolder1"), PlaceHolder)
        PlaceHolder1.Controls.Add(New LiteralControl("<table border='0' cellpadding='5' cellspacing='0' width='100%' align='center'><tr><td width='110px'>"))
        Dim lb_Description As New Label()
        lb_Description.Text = "Description: "
        lb_Description.ID = "lb_Description"
        PlaceHolder1.Controls.Add(lb_Description)
        Dim txt_Description As New RadTextBox()
        txt_Description.ID = "txt_Description"
        PlaceHolder1.Controls.Add(txt_Description)
 
        'To check if in Insert Mode
        If TypeOf e.Item Is GridEditFormInsertItem OrElse TypeOf e.Item Is GridDataInsertItem Then
 
            Dim btn_insert As New Button()
            btn_insert.Text = "Insert"
            btn_insert.ID = "btn_Insert"
            btn_insert.CommandName = "PerformInsert"
            PlaceHolder1.Controls.Add(btn_insert)
        Else
 
            Dim btn_update As New Button()
            btn_update.Text = "Update"
            btn_update.ID = "btn_Update"
            btn_update.CommandName = "Update"
            PlaceHolder1.Controls.Add(btn_update)
        End If
        Dim btn_cancel As New Button()
        btn_cancel.Text = "Cancel"
        btn_cancel.ID = "btn_cancel"
        btn_cancel.CommandName = "Cancel"
        btn_cancel.CssClass = "button"
        PlaceHolder1.Controls.Add(btn_cancel)
    End If
End Sub
Protected Sub RadGrid1_InsertCommand(sender As Object, e As GridCommandEventArgs)
    'Your code to Insert
End Sub
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    ' Your code to Update
End Sub

Thanks,
Princy
0
Duy
Top achievements
Rank 1
answered on 27 Aug 2013, 01:40 PM
thank you but what i'm trying to say is how do i get the "Add New Record" to open the form. Right now it keeps saying "Item in insert mode does implement indexer only when the edit form is autogenerated".

if i have the add button where i declared the edit and delete button, it doesn't do anything. so how do i open the form when click on the "Add new record" button at the command bar?

thanks
0
Duy
Top achievements
Rank 1
answered on 27 Aug 2013, 02:18 PM
got it. i just had to make sure that when it is in insert mode do not assign value to the control
If TypeOf e.Item Is GridEditFormInsertItem OrElse TypeOf e.Item Is GridDataInsertItem Then


end if
Tags
Grid
Asked by
Duy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Duy
Top achievements
Rank 1
Share this question
or