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

Getting Values code behind using insertcommand with grid edit mode set to popup

2 Answers 262 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chuck
Top achievements
Rank 1
Chuck asked on 31 Jan 2009, 06:44 PM

Here's my grid

<telerik:radgrid id="RadGrid1" autogenerateeditcolumn="False" runat="server" skin="Hay" AutoGenerateColumns="False">   
 
<PagerStyle Mode="NextPrevAndNumeric" /> 
 
 
 
<MasterTableView EditMode="PopUp" CommandItemDisplay="Top" DataKeyNames="UserID" AllowAutomaticDeletes="false">  
 
 
 
<Columns> 
 
 
 
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="UserID" HeaderText="UserID" DataField="UserID"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="FirstName" HeaderText="FirstName" DataField="FirstName"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="LastName" HeaderText="LastName" DataField="LastName"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="Email" HeaderText="E-mail" DataField="Email"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="Position" HeaderText="Position" DataField="Position"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="Salutation" HeaderText="Salutation" DataField="Salutation"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="Biography" HeaderText="Biography" DataField="Biography"></telerik:GridBoundColumn> 
 
 
 
<telerik:GridBoundColumn UniqueName="Password" HeaderText="Password" DataField="Password"></telerik:GridBoundColumn>   
 
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"></telerik:GridButtonColumn> 
 
 
 
</Columns> 
 
 
 
</MasterTableView> 
 
 
 
</telerik:radgrid> 
 


Here is my code behind

 

 

 

    Protected Sub RadGrid1_InsertCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand  
        Dim item As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)  
        Dim _user As SnapTech.Library.User = SnapTech.Library.User.NewUser  
        _user.FirstName = item.GetDataKeyValue("FirstName").ToString()  
        _user.LastName = item.GetDataKeyValue("LastName").ToString()  
        _user.Email = item.GetDataKeyValue("Email").ToString()  
        _user.Position = item.GetDataKeyValue("Position").ToString()  
        _user.Salutation = item.GetDataKeyValue("Salution").ToString()  
        _user.Biography = item.GetDataKeyValue("Biography").ToString()  
        _user.Password = item.GetDataKeyValue("Password").ToString()  
        _user.Save()  
    End Sub 

 


I get the following error in the codebehind at line "_user.FirstName = item.GetDataKeyValue("FirstName").ToString()
        Index was out of range. Must be non-negative and less than the size of the collection.  Parameter name: index

Whats the correct way for me to extract the values from the grid pop up form when the user clicks insert.

2 Answers, 1 is accepted

Sort by
0
Chuck
Top achievements
Rank 1
answered on 31 Jan 2009, 09:56 PM

I figured out how to get the data but now when use the command update I only get the previous value and not the value I just typed in.

        <telerik:radgrid id="RadGrid1" runat="server" skin="Hay" AutoGenerateColumns="False">                          
            <PagerStyle Mode="NextPrevAndNumeric" /> 
            <MasterTableView EditMode="EditForms" CommandItemDisplay="Top" DataKeyNames="UserID">  
                <Columns> 
                    <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn UniqueName="UserID" HeaderText="UserID" DataField="UserID"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="FirstName" HeaderText="FirstName" DataField="FirstName"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="LastName" HeaderText="LastName" DataField="LastName"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Email" HeaderText="E-mail" DataField="Email"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Position" HeaderText="Position" DataField="Position"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Salutation" HeaderText="Salutation" DataField="Salutation"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Biography" HeaderText="Biography" DataField="Biography"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="Password" HeaderText="Password" DataField="Password"></telerik:GridBoundColumn>                      
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"></telerik:GridButtonColumn> 
                </Columns> 
             <EditFormSettings EditFormType="Template">  
                    <FormStyle BackColor="#ECFED8" /> 
                    <EditColumn UniqueName="EditColumn" AutoPostBackOnFilter="true"></EditColumn>                      
                    <FormTemplate> 
                        <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0">  
                            <tr> 
                                <td> 
                                </td> 
                                <td> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    FirstName:</td> 
                                <td> 
                                    <asp:TextBox ID="txtFirstName" Text='<%# Bind( "FirstName") %>' runat="server" > 
                                    </asp:TextBox></td>  
                            </tr> 
                            <tr> 
                                <td> 
                                    Last Name:</td> 
                                <td> 
                                    <asp:TextBox ID="TextBox11" Text='<%# Bind( "LastName") %>' runat="server">  
                                    </asp:TextBox></td>  
                            </tr> 
                            <tr> 
                                <td> 
                                    E-mail:</td> 
                                <td> 
                                    <asp:TextBox ID="TextBox12" Text='<%# Bind( "Email") %>' runat="server">  
                                    </asp:TextBox></td>  
                            </tr> 
                            <tr> 
                                <td> 
                                    Position:</td> 
                                <td> 
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind( "Position" ) %>'>  
                                    </asp:TextBox></td>  
                            </tr> 
                            <tr> 
                                <td> 
                                    Salutation:</td> 
                                <td> 
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind( "Salutation") %>'>  
                                    </asp:TextBox></td>  
                            </tr> 
                            <tr> 
                                <td> 
                                    Biography:</td> 
                                <td> 
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind( "Biography") %>'>  
                                    </asp:TextBox></td>  
                            </tr> 
                         </table> 
                        <table style="width: 100%">  
                            <tr> 
                                <td align="right" colspan="2">  
                                    <asp:Button ID="Button1" Text='<%#IIf(TypeOf(Container) is GridEditFormInsertItem,"Insert","Update")%>' runat="server" CommandName='<%#IIf(TypeOf(Container) is GridEditFormInsertItem,"PerformInsert","Update")%>'</asp:Button>&nbsp;  
                                    <asp:Button ID="Button2" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel">  
                                    </asp:Button> 
                                </td> 
                            </tr> 
                        </table> 
                    </FormTemplate> 
                </EditFormSettings> 
            </MasterTableView> 
        </telerik:radgrid> 

    Protected Sub RadGrid1_UpdateCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand  
        If (e.Item.ItemType = GridItemType.EditFormItem) And (e.Item.IsInEditMode) Then 
            Dim editedItem As GridEditFormItem = CType(e.Item, GridEditFormItem)  
            Dim ID As Integer = CInt(editedItem.GetDataKeyValue("UserID"))  
            If ID <> 0 Then 
 
                Dim _user As SnapTech.Library.User = SnapTech.Library.User.GetUser(ID)  
                Dim txtFirstName As TextBox = CType(editItem.FindControl("txtFirstName"), TextBox)  
                _user.FirstName = txtFirstName.Text  
 
                _user.Save()  
 
                RadGrid1.DataBind()  
            End If 
 
 
        End If 
    End Sub 

_user.FirstName = txtFirstName.Text only gives me the data that was in the textbox before I changed it. It seems when I click the update button, the form is posting back resetting the data to the previous entries. I must be missing something.
0
Chuck
Top achievements
Rank 1
answered on 31 Jan 2009, 10:24 PM
Alrighty answer my own question after digging through the documentation some more. I was binding the datasource during page_load. What you need to do is use the following

    Protected Sub RadGrid1_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        Dim _users As Users = Users.GetUsers  
        RadGrid1.DataSource = _users  
    End Sub 

Now it appears RadGrid1 will only update the datasource when it needs to.
Tags
Grid
Asked by
Chuck
Top achievements
Rank 1
Answers by
Chuck
Top achievements
Rank 1
Share this question
or