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

Grid Insert Manual Databinding

9 Answers 251 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jan
Top achievements
Rank 1
Jan asked on 10 Aug 2009, 08:38 AM
Hello,

I want to insert a new record into more than one MS access data table at one time.
Therefor I am using manual databinding in codebehind on the insert operation.
I've read and tried every solution i've found in this forum without any luck.
I'm using:

<

 

EditFormSettings EditFormType="Template">

I've tried retrieving the insert data both in ItemCommand and in InsertCommand:

 

protected void detailsGrid_ItemCommand(object source, GridCommandEventArgs e)  
    {  
        if (e.CommandName == RadGrid.PerformInsertCommandName)  
        {  
            //GridEditableItem insertedItem = (GridEditableItem)e.Item;   
            GridEditFormInsertItem insertedItem = e.Item as GridEditFormInsertItem;  
            string LastName = (insertedItem["TextBox2"].Controls[0] as TextBox).Text;  
        }  
    } 
And:
 protected void detailsGrid_InsertCommand(object source, GridCommandEventArgs e)  
    {  
        //GridEditableItem insertedItem = e.Item as GridEditableItem;  
        GridEditFormInsertItem insertedItem = e.Item as GridEditFormInsertItem;  
        string LastName = (insertedItem["TextBox2"].Controls[0] as TextBox).Text;  
    } 
However insertedItem always returns Null.

Any suggestions are greatly appreciated

Regards

Janne P

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Aug 2009, 09:45 AM
Hello Janne,

You have to use the FindControl method to access the textbox in a grid template. So, try out the following code to access the TextBox control in the insert form:
c#:
protected void detailsGrid_InsertCommand(object source, GridCommandEventArgs e)   
    {   
        GridEditFormInsertItem insertedItem = e.Item as GridEditFormInsertItem;   
        string LastName = (insertedItem.FindControl("TextBox2"as TextBox).Text;   
    }  

Thanks
Shinu.
0
Jan
Top achievements
Rank 1
answered on 10 Aug 2009, 11:36 AM
Thank you very much Shinu for your quick reply!

However insertedItem still returns Null thus giving me the ugly "NullReferenceException" reply.
Do you or anybody have any other ideas?

Here's more details:

 

 

 
            <telerik:RadGrid ID="detailsGrid"   
                runat="server"   
                DataSourceID="ds_detailsGrid"   
                GridLines="None" 
                Width="512px"   
                ShowHeader="False"   
                Skin=""   
                EnableEmbeddedSkins="False"   
                AutoGenerateColumns="False" 
                AllowAutomaticUpdates="True"   
                AllowAutomaticInserts="False"   
                AllowAutomaticDeletes="True" 
                OnItemUpdated="detailsGrid_ItemUpdated"   
                OnItemInserted="detailsGrid_ItemInserted" 
                OnItemCommand="detailsGrid_ItemCommand"   
                OnDataBound="detailsGrid_OnDataBound" 
                OnPreRender="detailsGrid_PreRender" 
                OnInsertCommand="detailsGrid_InsertCommand" >

////

               <

 

MasterTableView CommandItemDisplay="Top" DataKeyNames="bokareId"

 

 

                   DataSourceID="ds_detailsGrid">

////        

 

                <

 

EditFormSettings EditFormType="Template">

 

 

 

 

 

                    <EditColumn Visible="false">

 

 

 

 

 

                    </EditColumn>

 

 

 

 

 

                    <FormTemplate>
                    //Edit form content

 

 

                    </FormTemplate>

 

 

 

 

 

                </EditFormSettings>

 

 

 

               

 
Thanks

//janne

 

0
Jan
Top achievements
Rank 1
answered on 11 Aug 2009, 07:57 AM
Hello again,

I've found out that it works when I use the default "Save New" but
it doesn't work when I use <CommandItemTemplate> buttons.
The reason that I need to use <CommandItemTemplate> is that I
want to use templates in the edit form.

Does this information give anybody any ideas?

Thanks

Janne P
0
Shinu
Top achievements
Rank 2
answered on 11 Aug 2009, 09:04 AM
Hello Janne,

It does not matter if you use Custom buttons in the CommandItemTemplate but inorder to trigger the InsertCommand, you should be having a button in the form template with its CommandName set to PerformInsert as shown below:
aspx:
 <EditFormSettings EditFormType="Template"
      <FormTemplate> 
         //  Edit Form content 
         <asp:Button ID="Button1" runat="server" CommandName="PerformInsert" Text="Save" /> 
      </FormTemplate> 
 </EditFormSettings> 

Thanks
Shinu.
0
Jan
Top achievements
Rank 1
answered on 11 Aug 2009, 11:32 AM
Thanks alot Shinu!

This did the trick :-)

So if I understand correct it's not possible to trigger the InsertCommand from inside
CommandItemTemplate?
Seems a little weird to me if so because for instance the UpdateEditedCommand is
triggered from inside the CommandItemTemplate.

Thanks again.

Janne
0
Tsvetoslav
Telerik team
answered on 12 Aug 2009, 09:28 AM
Hello Jan,

Certainly you can trigger the Insert command from the CommandItemTemplate of the grid, you just have to make sure that the button you have placed into it, has its CommandName property set to PerformInsert, as Shinu has rightly pointed out.

Regards,
Tsvetoslav
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.
0
Jan
Top achievements
Rank 1
answered on 12 Aug 2009, 11:33 AM
Hello Tsvetoslav!

Thank you for your reply. However I've done more tests and I don't manage
to get it to work from inside CommandItemTemplate. Maybe there's something else I'm missing.
My code:
<MasterTableView CommandItemDisplay="Top" DataKeyNames="bokareId" DataSourceID="ds_detailsGrid">  
<Columns> 
  <telerik:GridTemplateColumn UniqueName="TemplateColumn">  
    <ItemTemplate> 
      //itemtemplatecontent  
    </ItemTemplate> 
  </telerik:GridTemplateColumn> 
</Columns> 
<EditFormSettings EditFormType="Template">  
  <FormTemplate> 
    <asp:TextBox ID="Enamn" runat="server" Text='<%# Bind("fldEnamn") %>'></asp:TextBox> 
    <asp:LinkButton ID="btnSaveNew1" runat="server" CommandName="PerformInsert">  
      SaveNew1  
    </asp:LinkButton> 
  </FormTemplate> 
</EditFormSettings> 
<CommandItemTemplate> 
  <asp:LinkButton ID="btnSaveNew2" runat="server" CommandName="PerformInsert">  
    SaveNew2  
  </asp:LinkButton> 
</CommandItemTemplate> 
Code behind:
protected void detailsGrid_InsertCommand(object source, GridCommandEventArgs e)  
    {  
        //Get the GridEditFormInsertItem of the RadGrid       
        GridEditFormInsertItem insertedItem = e.Item as GridEditFormInsertItem;  
        string LastName = (insertedItem.FindControl("Enamn") as TextBox).Text;  
    } 


btnSaveNew1 is working but btnSaveNew2 inside <CommandItemTemplate is giving me
the "System.NullReferenceException was unhandled by user code" error.
Feels like there's something else wrong with my code..... Do you find it?

Thanks again!

Regards

Janne P
0
Shinu
Top achievements
Rank 2
answered on 12 Aug 2009, 12:15 PM
Hello Janne,

To access the insert item on clicking a button with its CommandName set as PerformInsert, you would have to set the grid in insert mode either dynamically or by using another button with its CommandName set as InitInsert in the CommandItemTemplate. Then on clicking the btnSaveNew2 (in the CommandItemTemplate), you can use the following code to access the Insert item:
c#:
protected void detailsGrid_InsertCommand(object source, GridCommandEventArgs e) 
    { 
 
        GridEditFormInsertItem insertItem = (GridEditFormInsertItem)RadGrid1.MasterTableView.GetInsertItem(); 
         
 
    } 

Hope this helps you understand better.
Shinu.
0
Jan
Top achievements
Rank 1
answered on 12 Aug 2009, 12:28 PM
Hello, hello!
This did it for me :-) Thank you!!!

I do set the grid in Insert Mode by using another button as you describe.
But what did the trick was your snippet:

GridEditFormInsertItem

 

insertItem = (GridEditFormInsertItem)detailsGrid.MasterTableView.GetInsertItem();
I haven't come across this code before anywhere else.

 


Thanks again for your time and effort!

Best regards

Janne
Tags
Grid
Asked by
Jan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jan
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or