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

populate dropdownlist in grid

1 Answer 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dingen
Top achievements
Rank 1
dingen asked on 18 Jan 2010, 01:04 PM
Hi,

How can i use a dropdownlist ( ddlCategory) to display the value in each row in non editmode?
How can i populate the ddlCategory with dsddl as dataset? (cant use sqldatasource here!)

reg.

D
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="InPlace.aspx.vb" Inherits="InPlace" %> 
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head id="Head1" runat="server"
</head> 
<body class="BODY"
    <form id="mainForm" runat="server"
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    </asp:ScriptManager> 
    <!-- content start --> 
 
    <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:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True" 
        AllowAutomaticInserts="True" PageSize="10" OnItemCommand="onitemcommand_radgrid1" 
        AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="sqldatasource2"
        <PagerStyle Mode="NextPrevAndNumeric" /> 
        <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames="ProductID" 
            DataSourceID="Sqldatasource2" HorizontalAlign="NotSet" AutoGenerateColumns="False"
            <Columns> 
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"
                    <ItemStyle CssClass="MyImageButton" /> 
                </telerik:GridEditCommandColumn> 
                <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" 
                    UniqueName="ProductName" ColumnEditorID="GridTextBoxColumnEditor1"
                </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn HeaderText="CategoryName"
                    <ItemTemplate> 
                        <asp:DropDownList ID="ddlCategory" runat="server" DataTextField="CategoryName" DataValueField="CategoryID"
                        </asp:DropDownList> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridNumericColumn DataField="UnitsInStock" HeaderText="Units In Stock" SortExpression="UnitsInStock" 
                    UniqueName="UnitsInStock" ColumnEditorID="GridNumericColumnEditor1"
                </telerik:GridNumericColumn> 
                <telerik:GridBoundColumn DataField="QuantityPerUnit" HeaderText="Quantity Per Unit" 
                    SortExpression="QuantityPerUnit" UniqueName="QuantityPerUnit" Visible="false" 
                    EditFormColumnIndex="1" ColumnEditorID="GridTextBoxColumnEditor2"
                </telerik:GridBoundColumn> 
                <telerik:GridCheckBoxColumn DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" 
                    UniqueName="Discontinued" EditFormColumnIndex="1"
                </telerik:GridCheckBoxColumn> 
                <telerik:GridTemplateColumn HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="TemplateColumn" 
                    EditFormColumnIndex="1"
                    <ItemTemplate> 
                        <asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <span> 
                            <telerik:RadNumericTextBox runat="server" ID="tbUnitPrice" Width="40px" DbValue='<%# Bind("UnitPrice") %>'
                            </telerik:RadNumericTextBox><span style="color: Red"><asp:RequiredFieldValidator 
                                ID="RequiredFieldValidator1" ControlToValidate="tbUnitPrice" ErrorMessage="*" 
                                runat="server"
                            </asp:RequiredFieldValidator></span
                    </EditItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow" 
                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" 
                    UniqueName="DeleteColumn"
                    <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                </telerik:GridButtonColumn> 
            </Columns> 
            <EditFormSettings ColumnNumber="2" CaptionDataField="ProductName" CaptionFormatString="Edit properties of Product {0}"
                <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 Order" UpdateText="Update record" 
                    UniqueName="EditCommandColumn1" CancelText="Cancel edit"
                </EditColumn> 
                <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> 
            </EditFormSettings> 
        </MasterTableView> 
        <ClientSettings> 
            <ClientEvents OnRowDblClick="RowDblClick" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
    <telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor1" runat="server" TextBoxStyle-Width="200px" /> 
    <telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor2" runat="server" TextBoxStyle-Width="150px" /> 
    <telerik:GridDropDownListColumnEditor ID="GridDropDownColumnEditor1" runat="server" 
        DropDownStyle-Width="110px" /> 
    <telerik:GridNumericColumnEditor ID="GridNumericColumnEditor1" runat="server" NumericTextBox-Width="40px" /> 
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server"
    </telerik:RadWindowManager> 
    <br /> 
    <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT ProductID, ProductName, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, Discontinued FROM [Products]" 
        runat="server"></asp:SqlDataSource> 
    </form> 
</body> 
</html> 
 

Imports Telerik.Web.UI 
Imports System 
Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Web.UI.WebControls 
Imports System.Configuration 
 
Imports System.Drawing 
 
Partial Class InPlace 
    Inherits System.Web.UI.Page 
 
    Dim ConnString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString 
 
    Public Function dsddl() As DataSet 
        Dim conn As SqlConnection = New SqlConnection(ConnString) 
        Dim adapter As SqlDataAdapter = New SqlDataAdapter 
        Dim myDataSet As DataSet = New DataSet 
 
        conn.Open() 
        Try 
 
            adapter.SelectCommand = New SqlCommand("SELECT * from Categories", conn) 
            adapter.Fill(myDataSet, "cats"
        Finally 
            conn.Close() 
        End Try 
        Return myDataSet 
 
    End Function 
 
 
 
    Protected Sub onitemcommand_radgrid1(ByVal source As ObjectByVal e As GridCommandEventArgs) 
        Dim item As GridItem 
        Dim ddl As DropDownList 
 
        item = e.Item 
        If e.CommandName = RadGrid.EditCommandName Then 
            RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace 
            'initialize dropdownlist 
            If item.ItemType = GridItemType.Item Then 
                ddl = CType(item.FindControl("ddlCategory"), DropDownList) 
                ddl.DataSource = dsddl() 
                ddl.DataTextField = "CategoryName" 
                ddl.DataValueField = "CategoryID" 
                ddl.DataBind() 
 
            End If 
 
        End If 
        If e.CommandName = RadGrid.InitInsertCommandName Then 
            RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms 
        End If 
    End Sub 
 
  
End Class 
 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Jan 2010, 01:30 PM
Hi Dingen,

Try the following code snippet in order to populate the dropdownlist from code.

VB:
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) 
    If TypeOf e.Item Is GridDataItem Then 
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)             
        Dim dropdown As DropDownList = DirectCast(item.FindControl("ddlCategory"), DropDownList) 
          ' Populate the dropdown  
    End If 
End Sub 

-Shinu.
Tags
Grid
Asked by
dingen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or