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

Bind dropdownlist in Radgrid

4 Answers 200 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 24 May 2013, 08:44 AM
i had put a dropdownlist in formtemplate for user to select value.
but how can i assign the selected value from db to the dropdownlist ?

           <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function RowDblClick(sender, eventArgs) {
                    sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
                }
            </script>
        </telerik:RadCodeBlock>
 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid_License">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid_License" LoadingPanelID="RadAjaxLoadingPanel1">
                        </telerik:AjaxUpdatedControl>
                        <telerik:AjaxUpdatedControl ControlID="divMsgs"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
        </telerik:RadAjaxLoadingPanel>
            <telerik:RadGrid ID="RadGrid_License" runat="server" CellSpacing="0"
            DataSourceID="ODS_License" GridLines="None" style="margin-top: 0px"
            AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" ShowStatusBar="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
            AllowAutomaticUpdates="True">
                <MasterTableView AutoGenerateColumns="False"
                    DataKeyNames="License_To_Operate_ID" DataSourceID="ODS_License"
                    AllowFilteringByColumn="False" AllowPaging="False"
                    CommandItemDisplay="Top">
 
                    <CommandItemSettings ExportToPdfText="Export to PDF" />
                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
                        Visible="True">
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
                        Visible="True">
                    </ExpandCollapseColumn>
 
                    <Columns>
                                    <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn DataField="License_To_Operate_ID"
                            FilterControlAltText="Filter License_Number column" HeaderText="License_To_Operate_ID"
                            SortExpression="License_To_Operate_ID" UniqueName="License_To_Operate_ID" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="License_Name"
                            FilterControlAltText="Filter License_Name column" HeaderText="License Name"
                            SortExpression="License_Name" UniqueName="License_Name">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="License_Number"
                            FilterControlAltText="Filter License_Number column" HeaderText="License Number"
                            SortExpression="License_Number" UniqueName="License_Number">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="License_Issuer"
                            FilterControlAltText="Filter License_Issuer column" HeaderText="License Issuer"
                            SortExpression="License_Issuer" UniqueName="License_Issuer">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Valid_From" DataType="System.DateTime"
                            FilterControlAltText="Filter Valid_From column" HeaderText="Valid From"
                            SortExpression="Valid_From" UniqueName="Valid_From" DataFormatString="{0: dd-MMM-yyyy}">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Valid_Till" DataType="System.DateTime"
                            FilterControlAltText="Filter Valid_Till column" HeaderText="Valid Till"
                            SortExpression="Valid_Till" UniqueName="Valid_Till" DataFormatString="{0: dd-MMM-yyyy}">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Issue_date" DataType="System.DateTime"
                            FilterControlAltText="Filter Issue_date column" HeaderText="Issue Date"
                            SortExpression="Issue_date" UniqueName="Issue_date" DataFormatString="{0: dd-MMM-yyyy}">
                        </telerik:GridBoundColumn>
                    </Columns>
                    <EditFormSettings EditFormType="Template">
                        <EditColumn FilterControlAltText="Filter EditCommandColumn1 column"
                            UniqueName="EditCommandColumn1">
                        </EditColumn>
    <FormTemplate>
 
        <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="1" rules="none"
            style="border-collapse: collapse;">
            <tr class="EditFormHeader">
            <td style="width:100px">
            <asp:Label ID="Label5" Text="License Type" runat="server"></asp:Label>                                                           
            </td>
            <td>
                <telerik:RadDropDownList ID="rdl_license_type" runat="server" DataSourceID="LDS_License_type" DefaultMessage="Please select..."
                 DataValueField="License_ID" DataTextField="License_name" >
                </telerik:RadDropDownList>  
                <asp:Label ID="Lb_License_ID" runat="server" text='<%# Bind("License_ID") %>'></asp:Label>
     
            </td>
            </tr>
            <tr >
            <td >
            <asp:Label ID="lb" Text="License Number" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:TextBox ID="tb_License_Number" runat="server" Text='<%# Bind("License_Number") %>' TabIndex="2"></asp:TextBox>                                                           
            </td>
            </tr>
            <tr>
            <td>
            <asp:Label ID="Label1" Text="License Issuer" runat="server"></asp:Label>
            </td>
            <td>
            <asp:TextBox ID="tb_License_Issuer" runat="server" Text='<%# Bind("License_Issuer") %>' TabIndex="3"></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td>
            <asp:Label ID="Label2" Text="Issue Date" runat="server"></asp:Label>
            </td>
            <td>
            <telerik:RadDatePicker ID="IssueDatePicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("Issue_Date") %>' TabIndex="4"></telerik:RadDatePicker>
            </td>
            </tr>
            <tr>
            <td>
            <asp:Label ID="Label3" Text="Valid From" runat="server"></asp:Label>
            </td>
            <td>
            <telerik:RadDatePicker ID="RadDatePicker_ValidFrom" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("Valid_From") %>' TabIndex="5"></telerik:RadDatePicker>
            </td>
            </tr>
                                                                        <tr>
            <td>
            <asp:Label ID="Label4" Text="Valid To" runat="server"></asp:Label>
            </td>
            <td>
            <telerik:RadDatePicker ID="RadDatePicker_ValidTill" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("Valid_Till") %>' TabIndex="6"></telerik:RadDatePicker>
            </td>
            </tr>
 
               <tr>
                <td align="right" colspan="2">
                    <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
                        runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' >
                    </asp:Button
                    <asp:Button ID="btnDelete" Text="Delete" runat="server" CausesValidation="False"
                        CommandName="Delete"></asp:Button>
                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                        CommandName="Cancel"></asp:Button>
 
                </td>
            </tr>
        </table>
    </FormTemplate>
</EditFormSettings>
                    <PagerStyle PageSizeControlType="RadComboBox" />
                </MasterTableView>
                <PagerStyle PageSizeControlType="RadComboBox" />
                <FilterMenu EnableImageSprites="False">
                </FilterMenu>
 
        </telerik:RadGrid>
        <asp:LinqDataSource ID="LDS_License_type" runat="server"
            ContextTypeName="dcLRDBDataContext" EntityTypeName=""
            TableName="db_Dictionary_Licenses">
            </asp:LinqDataSource>
        <asp:ObjectDataSource ID="ODS_License" runat="server"
            SelectMethod="GetLicenseArrayByUserID" TypeName="LRDB">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="0" Name="id" SessionField="SelectedUserID"
                    Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:LinqDataSource ID="LDS_License" runat="server"
            ContextTypeName="dcLRDBDataContext" EntityTypeName=""
            TableName="db_Employee_Licenses" Where="UserID == @UserID">
            <WhereParameters>
                <asp:SessionParameter Name="UserID" SessionField="SelectedUserID"
                    Type="Int32" DefaultValue="0"/>
            </WhereParameters>
        </asp:LinqDataSource>

VB code behind

Protected Sub RadGrid_License_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid_License.ItemCommand
 
 
     If e.CommandName = RadGrid.InitInsertCommandName Then   'Click Add New Record button           
         Dim editColumn As GridEditCommandColumn = CType(RadGrid_License.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
         editColumn.Visible = False
     ElseIf (e.CommandName = RadGrid.RebindGridCommandName AndAlso e.Item.OwnerTableView.IsItemInserted) Then
         e.Canceled = True
     Else
         Dim editColumn As GridEditCommandColumn = CType(RadGrid_License.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
         If Not editColumn.Visible Then  'Click Cancel button (both Insert and Update mode)
             editColumn.Visible = True
         End If
 
         Dim ddlLicense_id As RadDropDownList = DirectCast(e.Item.FindControl("rdl_license_type"), RadDropDownList)
         Dim txtLicense_Number As TextBox = DirectCast(e.Item.FindControl("tb_License_Number"), TextBox)
         Dim txtLicense_Issuer As TextBox = DirectCast(e.Item.FindControl("tb_License_Issuer"), TextBox)
         Dim dateIssue As RadDatePicker = DirectCast(e.Item.FindControl("IssueDatePicker"), RadDatePicker)
         Dim dateValidFrom As RadDatePicker = DirectCast(e.Item.FindControl("RadDatePicker_ValidFrom"), RadDatePicker)
         Dim dateValidTill As RadDatePicker = DirectCast(e.Item.FindControl("RadDatePicker_ValidTill"), RadDatePicker)
         Dim dc As New dcLRDBDataContext
         If e.CommandName = "PerformInsert" Then
 
             If Request.QueryString("EmployeeID") = "" Then  'New Employee
                 Dim rnd As New Random()
                 Dim tmpUserID As Integer 'allow 10 digit only
                 tmpUserID = rnd.Next(100000, 999999) & DateTime.Now.Hour & DateTime.Now.Minute
                 HttpContext.Current.Session("tmpUserID") = tmpUserID
 
                 dc.insertEmployeeLicense(tmpUserID.ToString, ddlLicense_id.SelectedValue, txtLicense_Number.Text.Trim, txtLicense_Issuer.Text.Trim, _
                                            dateIssue.SelectedDate, dateValidFrom.SelectedDate, dateValidTill.SelectedDate, "Y", DateTime.Now, HttpContext.Current.Session("UserID").ToString)
 
             Else
                 dc.insertEmployeeLicense(Request.QueryString("EmployeeID").ToString, ddlLicense_id.SelectedValue, txtLicense_Number.Text.Trim, txtLicense_Issuer.Text.Trim, _
                                            dateIssue.SelectedDate, dateValidFrom.SelectedDate, dateValidTill.SelectedDate, "Y", DateTime.Now, HttpContext.Current.Session("UserID").ToString)
             End If
 
         ElseIf e.CommandName = "Update" Then
             Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("License_To_Operate_ID")
             Dim uid As Integer
             If Request.QueryString("EmployeeID") = "" Then
                 uid = HttpContext.Current.Session("tmpUserID").ToString
             Else
                 uid = GetUserIDbyEmployeeID(Request.QueryString("EmployeeID")).ToString
             End If
 
             Dim rec = (From p In dc.db_Employee_Licenses Where p.UserID = uid And p.License_To_Operate_ID = lid).FirstOrDefault
             If Not rec Is Nothing Then
                 rec.License_ID = ddlLicense_id.SelectedValue
                 rec.License_Number = txtLicense_Number.Text.Trim
                 rec.License_Issuer = txtLicense_Issuer.Text.Trim
                 rec.Issue_date = dateIssue.SelectedDate
                 rec.Valid_From = dateValidFrom.SelectedDate
                 rec.Valid_Till = dateValidTill.SelectedDate
             End If
         ElseIf e.CommandName = "Delete" Then
             Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("License_To_Operate_ID")
             Dim uid As Integer
             If Request.QueryString("EmployeeID") = "" Then
                 uid = HttpContext.Current.Session("tmpUserID").ToString
             Else
                 uid = GetUserIDbyEmployeeID(Request.QueryString("EmployeeID")).ToString
             End If
             Dim rec2 = (From p In dc.db_Employee_Licenses Where p.UserID = uid And p.License_To_Operate_ID = lid).FirstOrDefault
             If Not IsNothing(rec2) Then
                 dc.db_Employee_Licenses.DeleteOnSubmit(rec2)
                 dc.SubmitChanges()
             End If
         End If
             dc.Dispose()
     End If
 End Sub
 Private Sub RadGrid_License_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid_License.ItemDataBound
     If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
         Dim item As GridEditableItem = e.Item
         'access/modify the edit item template settings here
         Dim list As RadDropDownList = item.FindControl("rdl_license_type")
         'list.DataSource = Country_values
         'list.DataBind()
         If (Not HttpContext.Current.Session("updatedValue") Is Nothing) Then
             list.SelectedValue = 2
             'HttpContext.Current.Session("updatedValue")
         End If
     ElseIf (TypeOf e.Item Is GridDataItem AndAlso Not e.Item.IsInEditMode AndAlso Page.IsPostBack) Then
         Dim item As GridDataItem = e.Item
         Dim label As Label = item.FindControl("Lb_License_ID")
 
         'update the label value
         label.Text = HttpContext.Current.Session("updatedValue")
     End If
 End Sub








4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 24 May 2013, 10:23 AM
Hi Joe,
 
I guess you want to access the selected row in the dropdown list.Please try the following code.
 
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)
             
{
  GridEditableItem edit=(GridEditableItem)e.Item;
  RadDropDownList ddl = (RadDropDownList)edit.FindControl("rdl_license_type");
  ddl.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "License_ID").ToString();
}
}

Thanks
Princy
0
Joe
Top achievements
Rank 1
answered on 29 May 2013, 01:00 AM
the code works fine... thanks a lot.
0
Darcy
Top achievements
Rank 1
answered on 12 Aug 2013, 04:38 PM
I have run into a snag with databinding to a dropdown on edit.  Usually it works perfectly.

However, we have a product list db table - products can be flagged as active / inactive.  Our dropdowns for order submit, and order edit only have active values available. 

When editing an order with a de-activated product (say the order was placed yesterday, and the product got deactivated today), I get an error because the selected product id in the order can't bind to the product dropdown because its not found.

How do I get around this - its not feasible to have the edit dropdown list contain all products (there are thousands).
0
Princy
Top achievements
Rank 1
answered on 02 Sep 2013, 09:09 AM
Hi Darcy,

I'm not clear about your requirement.I guess you want to get the value of the dropdown when updating the RadGrid.Please try the following code snippet.If this doesn't help,please elaborate on your requirement.

ASPX:
<telerik:GridTemplateColumn HeaderText="Flag">                     
    <EditItemTemplate>
       <telerik:RadComboBox runat="server" ID="RadComboBox1">
            <Items>
                <telerik:RadComboBoxItem Text="Active" />
                <telerik:RadComboBoxItem Text="Inactive" />
             </Items>
         </telerik:RadComboBox>
     </EditItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
       string datakeyvalue = edit.GetDataKeyValue("DataKeyNames").ToString();//Accessing the datakey to identify the particular row being updated
        RadComboBox combo = (RadComboBox)edit.FindControl("RadComboBox1");//Accessing the radcombobox in editmode
        string combovalue= combo.Text;//Accessing the value of radcombobox
        //You Code to Update
    }
}


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