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

RadGrid & SelectedValue issue

4 Answers 79 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zeyad
Top achievements
Rank 1
Zeyad asked on 04 Nov 2013, 12:07 PM
Hello all

I have the following RadGrid, where i have an EditForm template for editing and inserting, well, the Update command is working fine, however, when using SelectedValue in the DropDownList the insert form will not show when i click add new record, and when i remove the SelectedValue property, the insert form shows but wont work when i insert data, i double checked my sql statement for inserting and it works fine.

Any suggestions?

    <telerik:RadGrid ID="GridStaff"  runat="server" ShowStatusBar="True" 
        OnItemCommand="GridStaff_ItemCommand" OnItemUpdated="GridStaff_ItemUpdated"
        OnItemCreated="GridStaff_ItemCreated"
        OnUpdateCommand="GridStaff_UpdateCommand" OnItemInserted="GridStaff_ItemInserted"
        OnInsertCommand="GridStaff_InsertCommand" OnPreRender="GridStaff_PreRender"
    AutoGenerateColumns="False" PageSize="20" AllowSorting="True" AllowAutomaticInserts="True"
    AllowPaging="True" DataSourceID="SqlDataSourceStaff" AllowAutomaticDeletes="true"
        OnItemDataBound="GridStaff_ItemDataBound"
    AllowAutomaticUpdates="True" Width="835px" FilterType="CheckedList" AllowFilteringByColumn="True"
                GridLines="None" CellSpacing="0">
                <ExportSettings>
                    <Pdf>
                        <PageHeader>
                            <LeftCell Text="" />
                            <MiddleCell Text="" />
                            <RightCell Text="" />
                        </PageHeader>
                        <PageFooter>
                            <LeftCell Text="" />
                            <MiddleCell Text="" />
                            <RightCell Text="" />
                        </PageFooter>
                    </Pdf>
                </ExportSettings>
                 
                <MasterTableView DataSourceID="SqlDataSourceStaff" InsertItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnFirstPage" DataKeyNames="UserID" EditMode="EditForms" AutoGenerateColumns="false" AllowMultiColumnSorting="False" Width="100%" CommandItemDisplay="Top" Name="admStaff">
                <CommandItemSettings ShowAddNewRecordButton="true" AddNewRecordText="Add New Staff Member" ShowExportToPdfButton="true" />
                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
                        Visible="True">
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
                        Visible="True">
                    </ExpandCollapseColumn>
                <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                <HeaderStyle Width="20px">
                </HeaderStyle>
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn SortExpression="UserID" ShowFilterIcon="false" AllowFiltering="false" HeaderText="#" HeaderButtonType="TextButton" DataField="UserID" UniqueName="UserID" MaxLength="7">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="EnglishName" HeaderText="Name" HeaderButtonType="TextButton" DataField="EnglishName" UniqueName="EnglishName" MaxLength="40">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="Subject" HeaderText="Subject" HeaderButtonType="TextButton" DataField="SubEn" UniqueName="Subject">
                </telerik:GridBoundColumn>
                 
                <telerik:GridBoundColumn SortExpression="DepEn" HeaderText="Department" HeaderButtonType="TextButton" DataField="DepartID" UniqueName="DepEn">
                </telerik:GridBoundColumn>
                 
                <telerik:GridTemplateColumn HeaderText="Load/Week" UniqueName="TLoad" AllowFiltering="false" >
                <ItemStyle Width="20px" />
                    <ItemTemplate>
                        <asp:Label ID="lblTeacherLoad" runat="server"></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                 
                <telerik:GridBoundColumn SortExpression="PhoneNumber" HeaderText="PhoneNumber" HeaderButtonType="TextButton" AllowFiltering="false" DataField="PhoneNumber" UniqueName="PhoneNumber">
                </telerik:GridBoundColumn>
                 
                <telerik:GridTemplateColumn HeaderText="MSG" UniqueName="SendMSG" AllowFiltering="false" >
                <ItemStyle Width="20px" />
                    <ItemTemplate>
                        <asp:ImageButton ID="img_SendMSG" runat="server" CommandName="SendMsgToStaff" ImageUrl="~/images/sendmsgicon.png" CommandArgument='<%# Bind("UserID") %>' Width="20px" Height="20px" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="SMS" UniqueName="SendSMS" AllowFiltering="false" >
                <ItemStyle Width="20px" />
                    <ItemTemplate>
                        <asp:ImageButton ID="img_SendSMS" runat="server" CommandName="SendSmsToStaff" ImageUrl="~/images/sendsmsicon.png" CommandArgument='<%# Bind("UserID") %>' Width="20px" Height="20px" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                </Columns>
                 
                <EditFormSettings EditFormType="Template" PopUpSettings-Modal="true">
                         
                         
                        <FormTemplate>
                        <div class="register">
                         
                            
                        
                           <div class="divClass">
         
         
        <fieldset class="rowPersonalEn">
            <legend>Personal Details
            </legend>
            <p>
                <label>User ID
                </label>
                 
                <asp:TextBox ID="txtUserID" CssClass="long" Text='<%# Bind("UserID") %>' Enabled="false" runat="server"></asp:TextBox>
            </p>
            <p>
                <label>First Name
                </label>
                 
                <asp:TextBox ID="txtFirstNameEn" CssClass="long" Text='<%# Bind("FirstNameEn") %>' runat="server"></asp:TextBox>
            </p>
            <p>
                <label>Second Name
                </label>
                 
                <asp:TextBox ID="txtSecondNameEn" CssClass="long" Text='<%# Bind("SecondNameEn") %>' runat="server"></asp:TextBox>
            </p>
            <p>
                <label>Family Name
                </label>
                 
                <asp:TextBox ID="txtFamilyNameEn" CssClass="long" Text='<%# Bind("FamilyNameEn") %>' runat="server"></asp:TextBox>
            </p>
            <p>
                <label>Date of Birth
                </label>
                 
                <asp:TextBox ID="txtDOB" CssClass="long" Text='<%# Bind("DOB") %>' runat="server"></asp:TextBox>
            </p>
            <p>
                <label>Nationality
                </label>
                 
                <asp:DropDownList ID="ddlNationality" runat="server">
                </asp:DropDownList>
            </p>
            <p>
                <label>ID No.
                </label>
                 
                <asp:TextBox ID="txtIDNo" CssClass="long" Text='<%# Bind("IDNo") %>' runat="server"></asp:TextBox>
                 
            </p>
            <p>
                <label>Mobile
                </label>
                 
                <asp:TextBox ID="txtMobile" CssClass="long" Text='<%# Bind("PhoneNumber") %>' runat="server"></asp:TextBox>
                 
            </p>
            <p>
                <label>Email
                </label>
                 
                <asp:TextBox ID="txtEmail" CssClass="long" Text='<%# Bind("Email") %>' runat="server"></asp:TextBox>
                 
            </p>
            <p>
                <label>Notes
                </label>
                 
                <asp:TextBox ID="txtNotes" TextMode="MultiLine" Text='<%# Bind("Notes") %>' CssClass="multi" runat="server"></asp:TextBox>
                 
            </p>
        </fieldset>
        <fieldset class="rowPersonalAr">
            <legend>المعلومات الشخصية</legend>
            <p style="text-align:left">
                 
                <asp:TextBox ID="txtFirstNameAr" CssClass="long" Text='<%# Bind("FirstNameAr") %>' runat="server"></asp:TextBox>
                <label>الاسم الأول
                </label>
            </p>
           <p>
                 
                <asp:TextBox ID="txtSecondNameAr" CssClass="long" Text='<%# Bind("SecondNameAr") %>' runat="server"></asp:TextBox>
                <label>الاسم الثاني
                </label>
            </p>
            <p>
                 
                <asp:TextBox ID="txtFamilyNameAr" CssClass="long" Text='<%# Bind("FamilyNameAr") %>' runat="server"></asp:TextBox>
                <label>العائلة
                </label>
            </p>
        </fieldset>
         
        <fieldset class="rowAcademical">
            <legend>Academical Information
            </legend>
            <p>
                <label>Degree</label>
                <asp:DropDownList ID="ddlAcademicDegree" runat="server" DataSourceID="DataSourceAD" DataTextField="ADEn" DataValueField="ADID" AppendDataBoundItems="true" >
                    <asp:ListItem Selected="True" Text="Select" Value="0"></asp:ListItem>
                     
                </asp:DropDownList>
            </p>
            <p>
                <label>Experience
                </label>
                 
                <asp:TextBox ID="txtExp" CssClass="long" Text='<%# Bind("YearsOfExp") %>' runat="server"></asp:TextBox>
            </p>
            <p>
                <label>College/Uni.
                </label>
                 
                <asp:TextBox ID="txtUniv" CssClass="long" Text='<%# Bind("CollegeName") %>' runat="server"></asp:TextBox>
            </p>
            <p>
                 <label>Department</label>
                <asp:DropDownList ID="ddlDept" runat="server" DataSourceID="DataSourceDepartments"  DataTextField="DepartmentEn" DataValueField="DepartmentID" AppendDataBoundItems="true" >
                                       
                                            <asp:ListItem Selected="True" Text="Select" Value="0" >
                                            </asp:ListItem>
                     
                </asp:DropDownList>
            </p>
            <p>
                 <label>Stage</label>
                <asp:DropDownList ID="ddlStage" runat="server" DataSourceID="DataSourceStages" DataTextField="StageEn" DataValueField="StageID"  AppendDataBoundItems="true">
                      <asp:ListItem Selected="True" Text="Select" Value="0"></asp:ListItem>
                     
                </asp:DropDownList>
            </p>
            <p>
                 <label>Subject</label>
                <asp:DropDownList ID="ddlSubject" runat="server" DataSourceID="DataSourceSubjects"  DataTextField="SubEn" DataValueField="SubID" AppendDataBoundItems="true">
                    <asp:ListItem Selected="True" Text="Select" Value="0"></asp:ListItem>
                     
                </asp:DropDownList>
            </p>
            <p>
                 <label>Hired Date</label>
                <asp:TextBox ID="txtHireDate" CssClass="long" Text='<%# Bind("HiredDate") %>' runat="server"></asp:TextBox>
            </p>
        </fieldset>
        <div>
         <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                runat="server" CommandName="Update">
                            </asp:Button
                                 <asp:Button ID="btnCancel" Text="Close" runat="server" CausesValidation="False"
                                CommandName="Cancel"></asp:Button>
        </div>
 
        </div>
         
         </div>
                        </FormTemplate>
                    </EditFormSettings>
                    <BatchEditingSettings EditType="Cell" />
                    <PagerStyle AlwaysVisible="True" PageSizeControlType="RadComboBox" />
                </MasterTableView>
                <PagerStyle AlwaysVisible="True" PageSizeControlType="RadComboBox" />
                <FilterMenu EnableImageSprites="False">
                </FilterMenu>
                </telerik:RadGrid>
 
<asp:SqlDataSource ID="SqlDataSourceStaff" runat="server"
      ConnectionString="<%$ ConnectionStrings:EschoolTestV2ConnectionString %>"
      SelectCommand="SELECT usr.*, usr.FirstNameEn+' '+usr.FamilyNameEn AS EnglishName, dept.DepEn AS DepartID,dept.DepID AS DeparID,sub.SubEn,sub.SubID, ad.ADEn,st.StageEn FROM AttUsers usr,AttStages st, AttAcademicalDegrees ad, AttDepartments dept, AttSubjects sub WHERE st.StageID=usr.Stage AND ad.ADID=usr.AcademicalDegree AND sub.SubID=usr.Subject AND usr.Department=dept.DepID"
      UpdateCommand="UPDATE AttUsers SET [FirstNameEn]=@FirstNameEn, [FirstNameAr]=@FirstNameAr, [SecondNameEn]=@SecondNameEn,[SecondNameAr]=@SecondNameAr,[FamilyNameEn]=@FamilyNameEn,[FamilyNameAr]=@FamilyNameAr,[DOB]=@DOB,[IDNo]=IDNo,[PhoneNumber]=@PhoneNumber,[Email]=@Email,[Notes]=@Notes,[YearsOfExp]=@YearsOfExp,[CollegeName]=@CollegeName,[Department]=@DeparID,[Subject]=@Subject,[AcademicalDegree]=@AcademicalDegree,[Stage]=@Stage,[HiredDate]=@HiredDate WHERE UserID=@UserID"
      InsertCommand="INSERT INTO AttUsers (FirstNameAr,SecondNameAr,FamilyNameAr,FirstNameEn,SecondNameEn,FamilyNameEn,DOB,IDNo,PhoneNumber,Email,Notes,YearsOfExp,CollegeName,Department,Subject,AcademicalDegree,Stage,HiredDate) VALUES (@FirstNameAr,@SecondNameAr,@FamilyNameAr,@FirstNameEn,@SecondNameEn,@FamilyNameEn,@DOB,@IDNo,@PhoneNumber,@Email,@Notes,@YearsOfExp,@CollegeName,@DeparID,@Subject,@AcademicalDegree,@Stage,@HiredDate)">
        <UpdateParameters>
            <asp:Parameter Name="UserID" Type="String"></asp:Parameter>
            <asp:Parameter Name="FirstNameAr" Type="String"></asp:Parameter>
            <asp:Parameter Name="FirstNameEn" Type="String"></asp:Parameter>
            <asp:Parameter Name="SecondNameAr" Type="String"></asp:Parameter>
            <asp:Parameter Name="SecondNameEn" Type="String"></asp:Parameter>
            <asp:Parameter Name="FamilyNameAr" Type="String"></asp:Parameter>
            <asp:Parameter Name="FamilyNameEn" Type="String"></asp:Parameter>
            <asp:Parameter Name="DOB" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="IDNo" Type="String"></asp:Parameter>
            <asp:Parameter Name="PhoneNumber" Type="String"></asp:Parameter>
            <asp:Parameter Name="Email" Type="String"></asp:Parameter>
            <asp:Parameter Name="Notes" Type="String"></asp:Parameter>
            <asp:Parameter Name="YearsOfExp" Type="String"></asp:Parameter>
            <asp:Parameter Name="CollegeName" Type="String"></asp:Parameter>
            <asp:Parameter Name="DeparID" Type="String"></asp:Parameter>
            <asp:Parameter Name="Subject" Type="String"></asp:Parameter>
             
            <asp:Parameter Name="AcademicalDegree" Type="String"></asp:Parameter>
            <asp:Parameter Name="HiredDate" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="Stage" Type="String" />
        </UpdateParameters>
        <InsertParameters>
             
            <asp:Parameter Name="FirstNameAr" Type="String"></asp:Parameter>
            <asp:Parameter Name="FirstNameEn" Type="String"></asp:Parameter>
            <asp:Parameter Name="SecondNameAr" Type="String"></asp:Parameter>
            <asp:Parameter Name="SecondNameEn" Type="String"></asp:Parameter>
            <asp:Parameter Name="FamilyNameAr" Type="String"></asp:Parameter>
            <asp:Parameter Name="FamilyNameEn" Type="String"></asp:Parameter>
            <asp:Parameter Name="DOB" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="IDNo" Type="String"></asp:Parameter>
            <asp:Parameter Name="PhoneNumber" Type="String"></asp:Parameter>
            <asp:Parameter Name="Email" Type="String"></asp:Parameter>
            <asp:Parameter Name="Notes" Type="String"></asp:Parameter>
            <asp:Parameter Name="YearsOfExp" Type="String"></asp:Parameter>
            <asp:Parameter Name="CollegeName" Type="String"></asp:Parameter>
            <asp:Parameter Name="DeparID" Type="String"></asp:Parameter>
            <asp:Parameter Name="Subject" Type="String"></asp:Parameter>
            <asp:Parameter Name="Stage" Type="String"></asp:Parameter>
            <asp:Parameter Name="AcademicalDegree" Type="String"></asp:Parameter>
            <asp:Parameter Name="HiredDate" Type="DateTime"></asp:Parameter>
        </InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSourceDepartments" runat="server"
      ConnectionString="<%$ ConnectionStrings:EschoolTestV2ConnectionString %>"
      SelectCommand="SELECT DepEn AS DepartmentEn, DepID AS DepartmentID FROM AttDepartments ORDER BY DepID ASC">
    
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSourceSubjects" runat="server"
      ConnectionString="<%$ ConnectionStrings:EschoolTestV2ConnectionString %>"
      SelectCommand="SELECT SubEn, SubID FROM AttSubjects ORDER BY SubID ASC">
    
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSourceAD" runat="server"
      ConnectionString="<%$ ConnectionStrings:EschoolTestV2ConnectionString %>"
      SelectCommand="SELECT ADID, ADEn FROM AttAcademicalDegrees ORDER BY ADID ASC">
    
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSourceStages" runat="server"
      ConnectionString="<%$ ConnectionStrings:EschoolTestV2ConnectionString %>"
      SelectCommand="SELECT StageID, StageEn FROM AttStages ORDER BY StageID ASC">
    
</asp:SqlDataSource>

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 04 Nov 2013, 01:30 PM
Hi Zeyad,

You can set the selected value for the DropDownList in EditMode as follows :

C#:
protected void GridStaff_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode && e.Item is GridEditFormItem)
    {
        GridEditFormItem edit = (GridEditFormItem)e.Item;
        DropDownList list = (DropDownList)edit.FindControl("ddlAcademicDegree");
        list.SelectedValue = DataBinder.Eval(edit.DataItem, "ADEn").ToString();
    }
}

I have noticed that you are using Automatic CRUD Operation on your grid, then the usage of events like OnUpdateCommand, OnInsertCommand, OnDeleteCommand is not recommended. Please try removing it and use only Automatic CRUD. If you are Using Manual CRUD Operation please set properties AllowAutomaticInserts/Updates/Deletes to false and perform the operations completely from Code-behind.

Thanks,
Princy
0
Zeyad
Top achievements
Rank 1
answered on 04 Nov 2013, 02:21 PM
Thanks Princy,

That did work when changing .SelectedValue property to .SelectedItem.Text as it didn't read the ADID value which contains the proper Value, but it worked perfectly thanks.

But the weird thing that it doesn't accept insertion, and it doesn't even break or shows the Exception message, just the loader is fired infinitely.

Any suggestions?
0
Zeyad
Top achievements
Rank 1
answered on 04 Nov 2013, 02:54 PM
Also, when i hit the update button, it takes the values as NULL, i tried to update the parameter in the UpdateCommand function as the following

protected void GridStaff_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
         
            GridEditFormItem edit = (GridEditFormItem)e.Item;
            DropDownList listAD = (DropDownList)edit.FindControl("ddlAcademicDegree");
            SqlDataSourceStaff.UpdateParameters.Add("@AcademicalDegree", listAD.SelectedValue);
            DropDownList listStage = (DropDownList)edit.FindControl("ddlStage");
            SqlDataSourceStaff.UpdateParameters.Add("@Stage", listStage.SelectedValue);
            DropDownList listSubject = (DropDownList)edit.FindControl("ddlSubject");
            SqlDataSourceStaff.UpdateParameters.Add("@Subject", listSubject.SelectedValue);
            DropDownList listDept = (DropDownList)edit.FindControl("ddlDept");
            SqlDataSourceStaff.UpdateParameters.Add("@Department", listDept.SelectedValue);
         
            //GridStaff.DataSourceID = "SqlDataSourceStaff";
         
 
    }


With no luck.


Please advise
0
Princy
Top achievements
Rank 1
answered on 05 Nov 2013, 04:56 AM
Hi Zeyad,

The Insert is not firing because you haven't set the proper command name for the Insert and Update Button in the EditForm. Please set the Update/Insert button as follows:

ASPX:
<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
 runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />

To Update the values to DB and accept the value of DropDownList in InsertCommand, please try the following code snippet.

C#:
// To Update to DB
protected void GridStaff_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditFormItem edit = (GridEditFormItem)e.Item;
    string OrderID = edit.GetDataKeyValue("UserID").ToString();
    DropDownList listAD = (DropDownList)edit.FindControl("ddlAcademicDegree");
    string val=listAD.SelectedValue;
    SqlDataSource2.UpdateParameters.Add("AcademicalDegree", val);
    SqlDataSource2.UpdateCommand =" ";// Your Update query goes here
    SqlDataSource2.Update();
}
 
//Access the value of DropDownList during Insert
protected void GridStaff_InsertCommand(object sender, GridCommandEventArgs e)
{
    if (e.Item is GridEditFormItem)
    {
        GridEditFormItem edit = (GridEditFormItem)e.Item;
        DropDownList list = (DropDownList)edit.FindControl("ddlAcademicDegree");
        string val = list.SelectedValue;
        // Code to Insert to DB
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Zeyad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Zeyad
Top achievements
Rank 1
Share this question
or