Problem binding GridDropdownColumn with List

4 posts, 0 answers
  1. Trung Lee
    Trung Lee avatar
    5 posts
    Member since:
    Apr 2010

    Posted 19 Dec 2012 Link to this post

    I have problem in radgrid when biding data by List (not EntitiesDataSource or LinqDataSource or SqlDatasource).

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True"
            CellSpacing="0" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True"
            ShowStatusBar="True" OnDeleteCommand="RadGrid1_DeleteCommand" OnInsertCommand="RadGrid1_InsertCommand"
            OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
            GridLines="None" Skin="Metro" OnItemDataBound="RadGrid1_ItemDataBound">
            <ExportSettings ExportOnlyData="True" IgnorePaging="True" OpenInNewWindow="True">
                <Excel Format="Biff" />
            </ExportSettings>
            <MasterTableView CommandItemDisplay="Top" EditMode="InPlace" InsertItemPageIndexAction="ShowItemOnCurrentPage"
                AutoGenerateColumns="False" DataKeyNames="ID" GridLines="Both">
                <CommandItemSettings ExportToPdfText="Export to PDF" AddNewRecordText="Thêm mới"
                    ExportToExcelText="Xuất Excel" RefreshText="Làm mới" ShowExportToExcelButton="True">
                </CommandItemSettings>
                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridDropDownColumn DataField="ProviderId" DataType="System.Int64" FilterControlAltText="Filter ProviderId column"
                        HeaderText="Nhà cung cấp" SortExpression="ProviderId" ListTextField="ProviderName"
                        ListValueField="ProviderId" UniqueName="ProviderId" ColumnEditorID="GridDropDownColumnEditor1"
                        DropDownControlType="RadComboBox" runat="server">
                    </telerik:GridDropDownColumn>
               <%--     <telerik:GridTemplateColumn HeaderText="Nhà cung cấp" UniqueName="ProviderID">
                         <ItemTemplate>
                             <asp:DropDownList ID="ddlProvider" runat="server"/>
                         </ItemTemplate>
                     </telerik:GridTemplateColumn>--%>
                    <telerik:GridBoundColumn DataField="Buys" DataType="System.Decimal" FilterControlAltText="Filter Buys column"
                        HeaderText="Buys" SortExpression="Buys" UniqueName="Buys">
                    </telerik:GridBoundColumn>
                    <telerik:GridDateTimeColumn DataField="Date" DataType="System.DateTime" FilterControlAltText="Filter Date column"
                        HeaderText="Date" SortExpression="Date" UniqueName="Date">
                    </telerik:GridDateTimeColumn>
                    <telerik:GridBoundColumn DataField="Commen" FilterControlAltText="Filter Commen column"
                        HeaderText="Commen" SortExpression="Commen" UniqueName="Commen">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn ConfirmText="Bạn muốn xóa hóa đơn này ?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <ItemStyle CssClass="MyImageButton" Width="30px"></ItemStyle>
                    </telerik:GridEditCommandColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
        </telerik:RadGrid>

    And here is code behind


    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
     
    protected void RadGrid1_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        Int64 purchaseId = Convert.ToInt64((e.Item as GridDataItem).GetDataKeyValue("ID"));
        INPUTITEMDAO.Delete(purchaseId);
        RadGrid1.Rebind();
        //Reresh grid
        RadGrid1.MasterTableView.ClearEditItems();
    }
     
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        Hashtable values = new Hashtable();
        item.ExtractValues(values);
        INPUTITEM objInputItem=new INPUTITEM();
        item.UpdateValues(objInputItem);
     
        INPUTITEMDAO.InsertAndUpdate(objInputItem);
        RadGrid1.Rebind();
        RadGrid1.MasterTableView.ClearEditItems();
    }
     
    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        Int64 purchaseId =Convert.ToInt64(item.GetDataKeyValue("ID").ToString());
        INPUTITEM objInputItem = DataOut.GetAllINPUTITEMList().Where(i => i.ID == purchaseId).FirstOrDefault();
        item.UpdateValues(objInputItem);
        INPUTITEMDAO.InsertAndUpdate(objInputItem);
        RadGrid1.Rebind();
        RadGrid1.MasterTableView.ClearEditItems();
    }
     
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = DataOut.GetAllINPUTITEMList();
    }
     
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridDataItem dataItem = e.Item as GridDataItem;
        if (e.Item is GridEditableItem&& (e.Item as GridEditableItem).IsInEditMode)
        {
            GridEditManager editMan = editedItem.EditManager;
            GridDropDownListColumnEditor editor = editMan.GetColumnEditor("ProviderId") as GridDropDownListColumnEditor;
            //in case you have RadComboBox editor for the GridDropDownColumn (this is the default editor),  
            //you will need to use ComboBoxControl below instead of DropDownListControl
            //Then you can modify the list control as per your custom conventions
            if (editor != null)
            {
                editor.DataSource = PROVIDERDAO.GetAllPROVIDERList();
                editor.DataBind();
            }
        }
    }
    i don't know what problem that i run project not show ProviderId column but when i click edit it is show
    I Want grid show ProviderId column with ListTextField = "ProviderName" when page loading because eidt i had done ok.
    Please help me ?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Dec 2012 Link to this post

    Hi,

    Please try the following code snippet to show the dropdowncolumn value in view mode.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                DataRowView row = (DataRowView)e.Item.DataItem;
               item["Uniquenamee"].Text = row["ListTextField"].ToString();
            }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Trung Lee
    Trung Lee avatar
    5 posts
    Member since:
    Apr 2010

    Posted 19 Dec 2012 Link to this post

    Thanks Shinu! i'm trying follow your code snipet but make error: "Unable to cast object of type 'Librarys.REPORTSPAYMENT.INPUTITEM' to type 'System.Data.DataRowView".
    DataRowView row = (DataRowView)e.Item.DataItem;
    please help me?
  5. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 24 Dec 2012 Link to this post

    Hello Trung,

    You have to use slightly different approach in your case:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
               item["ColumnName"].Text = DataBinder.Eval(item.DataItem, "ListTextField").ToString();
            }
    }

    Best regards,
    Daniel
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top