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

DropDownList by ItemTemplate in RadGrid (Update data and Select item)

2 Answers 199 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sheila
Top achievements
Rank 1
Sheila asked on 04 May 2012, 09:33 AM
Hi,

I have a few questions to ask. I'm not able to update the data and select the item from the dropdownlist.
I have the code here. It is the exact same things i refer from ( http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultvb.aspx# ). Only change the dropdownlist to ItemTemplate
because it is not populate from the databinding.

<telerik:GridTemplateColumn HeaderText="Branch" SortExpression="Branch" UniqueName="TemplateColumn"
    EditFormColumnIndex="1">
    <ItemTemplate>
        <asp:Label runat="server" ID="lblBranch" ><%# DataBinder.Eval(Container.DataItem, "Branch")%></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
      <asp:DropDownList ID="DropDownList1" runat="server" >   
        <asp:ListItem Value="KL" >KL Head Office</asp:ListItem>       
        <asp:ListItem Value="PG" >Penang</asp:ListItem>  
        <asp:ListItem Value="JB" >Johor Bahru</asp:ListItem>
      </asp:DropDownList
    </EditItemTemplate
</telerik:GridTemplateColumn>

If I put SelectedValue='<%# Bind("Branch") %>'  at the EditItemTemplate I can update the data but
I cannot add new record. It will appear this error:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
And if I remove the SelectedValue, I can add new record but I cannot update the data.
It will appear this error:
User with ID 3 cannot be updated. Reason: Cannot insert the value NULL into column 'Branch', table 'eDMS42.dbo.TblBranchApprover'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
It's been bugging me for a week now. Please help me out.

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 May 2012, 09:42 AM
Hello Sheila,
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
  
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            DropDownList DropDownList1 = item.FindControl("DropDownList1") as DropDownList;
            string strBranch = item.GetDataKeyValue("Branch").ToString();
 
            if (DropDownList1.Items.FindByValue("strBranch") != null)
            {
                DropDownList1.Items.FindByValue("strBranch").Selected = true;
            }
 
        }    
}


<MasterTableView DataKeyNames="Branch">

Thanks,
Jayesh Goyani
0
Sheila
Top achievements
Rank 1
answered on 04 May 2012, 10:08 AM
Hi Jayesh,

Thanks for the code. Let's me show you the whole coding.

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            <!--
            function RowDblClick(sender, eventArgs) {
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
            }
  
            function gridCreated(sender, args) {
                if (sender.get_editIndexes && sender.get_editIndexes().length > 0) {
                    document.getElementById("OutPut").innerHTML = sender.get_editIndexes().join();
                }
                else {
                    document.getElementById("OutPut").innerHTML = "";
                }
            }
            -->
        </script>
    </telerik:RadCodeBlock>
      
    <div style="height: 20px; width: 350px;">
        <span style="font-weight: bold;"></span><span id="OutPut" style="font-weight: bold;
            color: navy;"></span>
    </div>
      
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadioButton1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="RadioButton2" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadioButton2">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="RadioButton1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="CheckBox1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
      
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
        <telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
        AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True"
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellSpacing="0">
        <PagerStyle Mode="NextPrevAndNumeric" />
  
        <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames="UserID"
            DataSourceID="SqlDataSource1" HorizontalAlign="NotSet" AutoGenerateColumns="False">
                <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                    <ItemStyle CssClass="MyImageButton" />
                </telerik:GridEditCommandColumn>
                  
                <telerik:GridDropDownColumn DataField="UserID" DataSourceID="SqlDataSource2"
                    HeaderText="Branch User" ListTextField="UserName" ListValueField="UserID"
                    UniqueName="UserID" ColumnEditorID="GridDropDownColumnEditor1">
                </telerik:GridDropDownColumn>
                  
                <telerik:GridTemplateColumn HeaderText="Branch" SortExpression="Branch" UniqueName="TemplateColumn"
                    EditFormColumnIndex="1">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblBranch" ><%# DataBinder.Eval(Container.DataItem, "Branch")%></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="DropDownList1" runat="server" >   
                        <asp:ListItem Value="KL" >KL Head Office</asp:ListItem>       
                        <asp:ListItem Value="PG" >Penang</asp:ListItem>  
                        <asp:ListItem Value="JB" >Johor Bahru</asp:ListItem>
                      </asp:DropDownList
                    </EditItemTemplate
                </telerik:GridTemplateColumn>
                  
                <telerik:GridNumericColumn DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy"
                    UniqueName="ModifiedBy" ReadOnly="true" ColumnEditorID="GridNumericColumnEditor1">
                </telerik:GridNumericColumn>
                                     
                <telerik:GridTemplateColumn HeaderText="Modified On" SortExpression="DateModified" UniqueName="TemplateColumn"
                    ReadOnly="true" EditFormColumnIndex="2">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblModifiedOn" Text='<%# Eval("DateModified","{0:MM/dd/yyyy hh:mm tt}") %>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                  
                <telerik:GridButtonColumn ConfirmText="Delete this user?" ConfirmDialogType="RadWindow"
                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                    UniqueName="DeleteColumn">
                    <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                </telerik:GridButtonColumn>
                </Columns>
  
            <EditFormSettings ColumnNumber="2" CaptionDataField="UserID" CaptionFormatString="Edit properties of User {0}"
                InsertCaption="New User">
                <FormTableItemStyle Wrap="False"></FormTableItemStyle>
                <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
                <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="White" Width="100%" />
                <FormTableStyle CellSpacing="0" CellPadding="2" Height="110px" BackColor="White" />
                <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
                <EditColumn ButtonType="ImageButton" InsertText="Insert" UpdateText="Update record"
                    UniqueName="EditCommandColumn1" CancelText="Cancel edit">
                </EditColumn>
                <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
            </EditFormSettings>
        </MasterTableView>
          
        <ClientSettings>
            <ClientEvents OnRowDblClick="RowDblClick" OnGridCreated="gridCreated" />
        </ClientSettings>
          
<FilterMenu EnableImageSprites="False"></FilterMenu>
    </telerik:RadGrid>
    <telerik:GridDropDownListColumnEditor ID="GridDropDownColumnEditor1" runat="server" DropDownStyle-Width="170px" />
    <telerik:GridNumericColumnEditor ID="GridNumericColumnEditor1" runat="server" NumericTextBox-Width="40px" />
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
    </telerik:RadWindowManager>
    <br />
      
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:eDMS42ConnectionString %>"
        SelectCommand="SELECT * FROM [TblBranchApprover]" 
        InsertCommand="INSERT INTO [TblBranchApprover] ([UserID], [Branch]) VALUES (@UserID, @Branch)" 
        DeleteCommand="DELETE FROM [TblBranchApprover] WHERE [UserID] = @UserID"
        UpdateCommand="UPDATE [TblBranchApprover] SET [Branch] = @Branch WHERE [UserID] = @UserID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="UserID" Type="int32" />
            <asp:Parameter Name="Branch" Type="string" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="UserID" Type="int32" />
            <asp:Parameter Name="Branch" Type="string" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:eDMS42ConnectionString %>"
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT [UserID], [UserName] FROM [TblUser]">
    </asp:SqlDataSource>

Thanks.
Tags
Grid
Asked by
Sheila
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Sheila
Top achievements
Rank 1
Share this question
or