Working with GridDropDownColumn and dynamic datasource setting

8 posts, 0 answers
  1. David Thompson
    David Thompson avatar
    29 posts
    Member since:
    Feb 2010

    Posted 04 Oct 2010 Link to this post

    All,

    I am having problems getting my GridDropDownColumn to bind with the current value of my Grid data row.  I can get the drop down to bind to it's source but I can't seem to get it to set the correct selected value automatically.

    Here is my ASPC Code:
    <%@ Page Language="C#" MasterPageFile="~/MasterPages/Popup.Master" AutoEventWireup="true" CodeBehind="CompanyItemReference.aspx.cs" Inherits="NTE.NTEx.Web.App.ItemMaster.CompanyItemReference" Title="Item Reference" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
    <center><asp:Label ID="Label1" CssClass="NormalTextBold" runat="server" ></asp:Label></center>
           <telerik:RadGrid ID="RadGrid1" Skin="Web20" runat="server" Width="100%" AutoGenerateColumns="False" HorizontalAlign="Right"
                BorderStyle="None" BorderWidth="0px" CellPadding="0" CellSpacing="0" ItemStyle-Height="10px" HeaderStyle-Height="10px"
                OnNeedDataSource="RadGrid1_NeedDataSource" 
                OnDeleteCommand="RadGrid1_DeleteCommand"
                OnInsertCommand="RadGrid1_InsertCommand"
                OnUpdateCommand="RadGrid1_UpdateCommand"
                OnItemDataBound="RadGrid1_ItemDataBound"
                PageSize="20" AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True" GridLines="Horizontal" ShowStatusBar="false">
                <PagerStyle Mode="NumericPages"></PagerStyle>
                <ClientSettings AllowExpandCollapse="True">
                   <Resizing AllowColumnResize="true" ResizeGridOnColumnResize="false" AllowRowResize="true" />
                </ClientSettings
                <MasterTableView CommandItemDisplay="Top" AllowFilteringByColumn="True" DataKeyNames="item_id" AllowMultiColumnSorting="True"
                    HierarchyLoadMode="ServerOnDemand" Width="100%" HierarchyDefaultExpanded="False" runat="server" 
                    Name="Item">
                    <DetailTables>
                        <telerik:GridTableView AllowPaging="false" CommandItemDisplay="Top" DataKeyNames="id" Width="80%" runat="server" Name="Reference">
                            <ParentTableRelation>
                                <telerik:GridRelationFields DetailKeyField="item_id" MasterKeyField="item_id" />
                            </ParentTableRelation>
                            <Columns>
                                <telerik:GridEditCommandColumn Resizable="false" HeaderStyle-Width="30px" ItemStyle-Width="30px" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="id" HeaderText="Detail ID"
                                    DataField="id">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="Item_ID" HeaderText="Item ID"
                                    DataField="item_id">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="HoldReferenceType"
                                    DataField="reference_type">
                                </telerik:GridBoundColumn>
                                <telerik:GridDropDownColumn 
                                    UniqueName="DDReferenceType" SortExpression="display_name" 
                                    ListTextField="display_name" EnableEmptyListItem = "true" EmptyListItemText="--Choose an option--" EmptyListItemValue=""
                                    ListValueField ="Reference_Type" HeaderText="Reference Type" DataField="reference_type">
                                </telerik:GridDropDownColumn>                            
                                <telerik:GridBoundColumn ReadOnly="true" UniqueName="reference_name" HeaderText="Name"
                                    DataField="reference_name">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="reference_value" HeaderText="Value"
                                    DataField="reference_value">
                                </telerik:GridBoundColumn>
                                        <telerik:GridButtonColumn ConfirmText="Delete this Item?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1">
                                            <HeaderStyle Width="20px" />
                                            <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                                        </telerik:GridButtonColumn>
                      </Columns>
                            <SortExpressions>
                                <telerik:GridSortExpression FieldName="reference_name"></telerik:GridSortExpression>
                            </SortExpressions>
                       </telerik:GridTableView>
                    </DetailTables>
                    <Columns>
                      <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="item_id" HeaderText="item_ID" 
                            DataField="item_id">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="MemNo" HeaderText="Member" 
                            DataField="Mem_No">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="GroupID" HeaderText="Community" 
                            DataField="Group_Id">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="type" 
                            HeaderText="Type"
                            DataField="owner_reference_type">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="reference" 
                            HeaderText="Product ID" FilterControlWidth="50px"
                            HeaderStyle-Width="100px" ItemStyle-Width="100px"
                            DataField="owner_reference_value">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Commodity" 
                            HeaderText="Commodity" ReadOnly="true"
                            ShowFilterIcon="false" FilterControlAltText="Enter a commodity and hit return" FilterControlWidth="20px" AutoPostBackOnFilter="true"
                            HeaderStyle-Width="30px" ItemStyle-Width="30px"
                            DataField="Commodity_code">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" UniqueName="UPC" 
                            HeaderText="UPC" Resizable="true" 
                             HeaderStyle-Width="120px" ItemStyle-Width="120px"
                           DataField="UPC">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" UniqueName="GTIN" 
                            HeaderText="GTIN"
                            HeaderStyle-Width="120px" ItemStyle-Width="120px"
                            DataField="GTIN">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" UniqueName="Branch" 
                            HeaderText="Branch" 
                            ShowFilterIcon="false" FilterControlAltText="Enter a branch and hit return" FilterControlWidth="20px" AutoPostBackOnFilter="true"
                            HeaderStyle-Width="30px" ItemStyle-Width="30px"
                            DataField="Branch">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ReadOnly="true" UniqueName="Description" 
                            HeaderText="Description" AllowFiltering="false"
                            DataField="Description">
                        </telerik:GridBoundColumn>
                        <telerik:GridButtonColumn ConfirmText="Delete this Item?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1">
                             <HeaderStyle Width="20px" />
                             <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                        </telerik:GridButtonColumn>
                   </Columns>
                            <SortExpressions>
                                <telerik:GridSortExpression FieldName="owner_reference_value"></telerik:GridSortExpression>
                            </SortExpressions>
                </MasterTableView>
            </telerik:RadGrid>
    </asp:Content>

    Here is my ASPX C# code:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using NTE.NTEx.Web.Common;
    using NTE.NTEx.BusinessLogic;
    using Telerik.Web.UI;
    using SubSonic; //Query Object
    using NTE.NTEx.Data;
    using NTE.NTEx.Web.Session;
    using NTE.NTEx.DataAccessObjects;
    using NTE.NTEx.Security;
    using System.Text;
      
    namespace NTE.NTEx.Web.App.ItemMaster
    {
        [Page("Item References", Description = "Item References")]
        public partial class CompanyItemReference : BasePage
        {
            private void Page_Load(object sender, System.EventArgs e)
            {
               if (!Page.IsPostBack)//first time hit page
                {
                    SetData("MemNo", Request.QueryString["MemNo"]);
                    SetData("GroupID", Request.QueryString["GroupID"]);
                    SetData("ReadOnly", Request.QueryString["ReadOnly"]);
                    if (string.IsNullOrEmpty(Data["WHERE"]) == true
                    {
                        SetWhereClause();
                    }
                }
            }
            protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            {
                LoadData();
            }
      
            private void SetWhereClause()
            {
                StringBuilder strSQL = new StringBuilder();
                DbParams parameters = new DbParams();
      
                strSQL.Append(" Select get_mem_group_filter (:1, :2, 'mem_no','group_id') where_clause from ntex_session" );
                parameters.Add("1", Data["MemNo"]);
                parameters.Add("2", Data["GroupId"]);
      
                DataSet ds = DataHelper.FetchData(strSQL.ToString(), parameters);
                SetData("WHERE", (string) ds.Tables[0].Rows[0]["where_clause"]);
            }
      
            private void LoadData()
            {
                StringBuilder strSQL = new StringBuilder();
                //Category Query
                DbParams parameters = new DbParams();
      
                strSQL.Append(" select * from company_item2");
                strSQL.Append(" where mem_no = :1 ");
                strSQL.Append(Data["Where"]);
                parameters.Add("1", Data["MemNo"]);
                DataSet ds = DataHelper.FetchData(strSQL.ToString(), parameters);
      
                RadGrid1.AutoGenerateColumns = false;
                RadGrid1.DataSource = ds.Tables[0];
                RadGrid1.MasterTableView.DataSource = ds.Tables[0];
                  
                strSQL.Remove(0, strSQL.Length);
                parameters.Clear();
      
                strSQL.Append(" select * from company_item");
                strSQL.Append(" where mem_no = :1 ");
                strSQL.Append(Data["Where"]);
                parameters.Add("1", Data["MemNo"]);
      
                ds = DataHelper.FetchData(strSQL.ToString(), parameters);
      
                RadGrid1.AutoGenerateColumns = false;
                RadGrid1.MasterTableView.DetailTables[0].DataSource = ds.Tables[0];
            }
            protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
            {
                string tableName = e.Item.OwnerTableView.Name.ToString();
                if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode && tableName=="Reference")
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    GridEditManager editMan = editedItem.EditManager;
                    StringBuilder strSQL = new StringBuilder();
      
                    strSQL.Append(" select a.reference_type, a.display_name from reference_master_type a where reference_master_id = to_number(getcontextattribute('"+Data["MemNo"]+"',"+Data["GroupId"]+",'REFMSTRID'))");
                    DataSet ds = DataHelper.FetchData(strSQL.ToString());
                    //
                    GridDropDownListColumnEditor editor = editMan.GetColumnEditor("DDReferenceType") 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
                    editor.DataSource = ds.Tables[0];
                    editor.DataBind();
                    //editor.Visible = true;
                    //editor.ComboBoxControl.SelectedValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString();
                    //And so on
                }
            }
            protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                //Get the GridEditableItem of the RadGrid  
                string tableName = e.Item.OwnerTableView.Name.ToString();
                GridEditableItem editedItem = e.Item as GridEditableItem;
                //try
                //{
                    switch (tableName)
                    {
                        case "Item":
                            {
                                string str_Reference = (editedItem["reference"].Controls[0] as TextBox).Text;
                                ItemCollection colIC = new ItemCollection();
                                Query qry = new Query(Item.Schema);
                                qry.AddWhere("owner_reference", str_Reference);
                                colIC.LoadAndCloseReader (qry.ExecuteReader());
                                if (colIC.Count == 1)
                                {
                                    ItemCorporationUsageCollection colICU = new ItemCorporationUsageCollection();
                                    qry = new Query(ItemCorporationUsage.Schema);
                                    qry.AddWhere("item_id", colIC[0].Id).AND("related_corporation_id", int.Parse(Data["MemNo"]));
                                    colICU.LoadAndCloseReader (qry.ExecuteReader());
      
                                    if (colICU.Count == 0)
                                    {
                                        DataAccessObjects.ItemCorporationUsage oICU;
                                        oICU = new ItemCorporationUsage();
                                        oICU.RelatedCorporationId = int.Parse(Data["MemNo"]);
                                        oICU.ItemId = colIC[0].Id;
                                        oICU.Save();
                                    }
                                }
                                LoadData();
                                DisplayMessage("Add Successfull.");
                                break;
                            }
                        case "Reference":
                            {
                                int itemid = int.Parse(editedItem.OwnerTableView.ParentItem.OwnerTableView.DataKeyValues[editedItem.OwnerTableView.ParentItem.ItemIndex]["item_id"].ToString());
                                DropDownList list = editedItem.FindControl("DDReferenceType") as DropDownList;
                                Response.Write(list.Items.Count +"<BR>");
                                Response.Write(editedItem["DDReferenceType"].Controls[0].ToString());
                                DataAccessObjects.ItemCorporationReference oICR;
                                oICR = new ItemCorporationReference();
                                oICR.RelatedCorporationId = int.Parse(Data["MemNo"]);
                                oICR.ItemId = itemid;
                                //oICR.ReferenceType = (editedItem.FindControl("DDReferenceType") as DropDownList).SelectedItem.Value;
                                oICR.Reference = (editedItem["reference_value"].Controls[0] as TextBox).Text;
                                //oICR.Save();
                                //LoadData();
                                DisplayMessage("Insert Successfull.");
                                break;
                            }
                    }
                //}
                //catch
                //{
                //    Response.Write("Error: Parameter could not be saved.");
                //}
            }
            protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.EditCommandName)
                {
                    Response.Write("Primary key for the clicked item from ItemCommand: " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["item_id"] + "<br>");
                    Response.Write("Item Index for the clicked item from ItemCommand: " + e.Item.ItemIndex + "<br>");
                    DisplayMessage("Table Name clicked item from ItemCommand: " + e.Item.OwnerTableView.Name.ToString() + "<br>");
                }
            }
            protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                //Get the GridEditableItem of the RadGrid  
                GridEditableItem editedItem = e.Item as GridEditableItem;
                string tableName = e.Item.OwnerTableView.Name.ToString();
                try
                {
                    switch (tableName)
                    {
                        case "Item":
                            {
                                int ItemID = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["item_id"].ToString());
                                Query qry = new Query(ItemCorporationReference.Schema);
                                qry.QueryType = QueryType.Delete;
                                qry.AddWhere("item_id", ItemID).AND("related_corporation_id", int.Parse(Data["MemNo"]));
                                qry.Execute();
      
                                qry = new Query(ItemCorporationUsage.Schema);
                                qry.QueryType = QueryType.Delete;
                                qry.AddWhere("item_id", ItemID).AND("related_corporation_id", int.Parse(Data["MemNo"]));
                                qry.Execute();
      
                                LoadData();
                                DisplayMessage("Delete Successfull.");
                                break;
                            }
                        case "Reference":
                            {
                                int DeleteID = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString());
                                DataAccessObjects.ItemCorporationReference.Delete(DeleteID);
                                DisplayMessage("Delete Successfull.");
                                break;
                            }
                    }
                }
                catch
                {
                    DisplayMessage("Error: Parameter could not be saved.");
                }
            }
            protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                //Get the GridEditableItem of the RadGrid  
                GridEditableItem editedItem = e.Item as GridEditableItem;
                string tableName = e.Item.OwnerTableView.Name.ToString();
                try
                {
                    switch (tableName)
                    {
                        case "Item":
                            {
                                break;
                            }
                        case "Reference":
                            {
                                int UpdateID = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString());
                                DataAccessObjects.ItemCorporationReference oICR;
                                oICR = new ItemCorporationReference(UpdateID);
                                oICR.ReferenceType = (editedItem["DDReferenceType"].Controls[0] as DropDownList).SelectedItem.Value;
                                oICR.Reference = (editedItem["reference_value"].Controls[0] as TextBox).Text;
                                oICR.Save();
                                DisplayMessage("Delete Successfull.");
                                break;
                            }
                    }
                }
                catch
                {
                    DisplayMessage("Error: Parameter could not be saved.");
                }
            }
            private void DisplayMessage(string text)
            {
                RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
            }
        }
    }
  2. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 06 Oct 2010 Link to this post

     Hi David Thompson,

    You may set the selected value of the combo box in the following way:

    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            string tableName = e.Item.OwnerTableView.Name.ToString();
            if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode && tableName == "Reference")
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                StringBuilder strSQL = new StringBuilder();
      
                strSQL.Append(" select a.reference_type, a.display_name from reference_master_type a where reference_master_id = to_number(getcontextattribute('" + Data["MemNo"] + "'," + Data["GroupId"] + ",'REFMSTRID'))");
                DataSet ds = DataHelper.FetchData(strSQL.ToString());
                // 
                GridDropDownListColumnEditor editor = editMan.GetColumnEditor("DDReferenceType") 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 
                editor.DataSource = ds.Tables[0];
                editor.DataBind();
                //editor.Visible = true; 
                editor.ComboBoxControl.SelectedValue = DataBinder.Eval(e.Item.DataItem,"reference_type").ToString(); 
                //And so on 
            }
        }

    You need to get the relevant value of the field set in DataFiled property of the GridDropDownColumn since it points to a field in the grid datasource that holds the values for the combo box, for this purpose we use Databinder.Eval. But have in mind that this will only bind the combo box when the row is in EditMode, when the grid is in normal mode there will be no data in this cell. You may fix this with additional dynamic databinding of the controls in the cell (in the place of the ComboBox) when grid is in display mode. But a better aproach is to bind declaratively the GridDropDownColumn to the datasource. This way it will automatically show the selected index and fill the cell in normal mode.

    <telerik:GridDropDownColumn UniqueName="DDReferenceType"
       DataSourceID="MyDataSource1"
       SortExpression="display_name" ListTextField="display_name"
       EnableEmptyListItem="true" EmptyListItemText="--Choose an option--"
       EmptyListItemValue="" ListValueField ="Reference_Type" HeaderText="Reference Type"
       DataField="reference_type"
    </telerik:GridDropDownColumn>
    <asp:SqlDataSource ID="MyDataSource1" runat="server" ConnectionString="<%$  ConnectionStrings:ConnectionString %>"
           SelectCommand="select a.reference_type, a.display_name from reference_master_type"></asp:SqlDataSource>

    Best wishes,

    Marin
    the Telerik team

     

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David Thompson
    David Thompson avatar
    29 posts
    Member since:
    Feb 2010

    Posted 06 Oct 2010 Link to this post

    Thanks! 

    That has worked.  But not I am having problems getting the value of the control when I do an INSERT or UPDATE.  Here is the error I am getting:

    Object reference not set to an instance of an object. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
      
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
      
    Source Error: 
      
      
    Line 148:                            int itemid = int.Parse(editedItem.OwnerTableView.ParentItem.OwnerTableView.DataKeyValues[editedItem.OwnerTableView.ParentItem.ItemIndex]["item_id"].ToString());
    Line 149:                            DropDownList list = editedItem.FindControl("DDReferenceType") as DropDownList;
    Line 150:                            Response.Write(list.Items.Count +"<BR>");
    Line 151:                            Response.Write(editedItem["DDReferenceType"].Controls[0].ToString());
    Line 152:                            DataAccessObjects.ItemCorporationReference oICR
      


    Here is the INSERT C# Logic:

    protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //Get the GridEditableItem of the RadGrid  
        string tableName = e.Item.OwnerTableView.Name.ToString();
        GridEditableItem editedItem = e.Item as GridEditableItem;
        //try
        //{
            switch (tableName)
            {
                case "Item":
                    {
                        string str_Reference = (editedItem["reference"].Controls[0] as TextBox).Text;
                        ItemCollection colIC = new ItemCollection();
                        Query qry = new Query(Item.Schema);
                        qry.AddWhere("owner_reference", str_Reference);
                        colIC.LoadAndCloseReader (qry.ExecuteReader());
                        if (colIC.Count == 1)
                        {
                            ItemCorporationUsageCollection colICU = new ItemCorporationUsageCollection();
                            qry = new Query(ItemCorporationUsage.Schema);
                            qry.AddWhere("item_id", colIC[0].Id).AND("related_corporation_id", int.Parse(Data["MemNo"]));
                            colICU.LoadAndCloseReader (qry.ExecuteReader());
                            if (colICU.Count == 0)
                            {
                                DataAccessObjects.ItemCorporationUsage oICU;
                                oICU = new ItemCorporationUsage();
                                oICU.RelatedCorporationId = int.Parse(Data["MemNo"]);
                                oICU.ItemId = colIC[0].Id;
                                oICU.Save();
                            }
                        }
                        LoadData();
                        DisplayMessage("Add Successfull.");
                        break;
                    }
                case "Reference":
                    {
                        int itemid = int.Parse(editedItem.OwnerTableView.ParentItem.OwnerTableView.DataKeyValues[editedItem.OwnerTableView.ParentItem.ItemIndex]["item_id"].ToString());
                        DropDownList list = editedItem.FindControl("DDReferenceType") as DropDownList;
                        Response.Write(list.Items.Count +"<BR>");
                        Response.Write(editedItem["DDReferenceType"].Controls[0].ToString());
                        DataAccessObjects.ItemCorporationReference oICR;
                        oICR = new ItemCorporationReference();
                        oICR.RelatedCorporationId = int.Parse(Data["MemNo"]);
                        oICR.ItemId = itemid;
                        //oICR.ReferenceType = (editedItem.FindControl("DDReferenceType") as DropDownList).SelectedItem.Value;
                        oICR.Reference = (editedItem["reference_value"].Controls[0] as TextBox).Text;
                        //oICR.Save();
                        //LoadData();
                        DisplayMessage("Insert Successfull.");
                        break;
                    }
            }
        //}
        //catch
        //{
        //    Response.Write("Error: Parameter could not be saved.");
        //}
    }
  5. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 07 Oct 2010 Link to this post

    Hi David Thompson,

    It seems that the error is thrown at this line:

    Line 149: DropDownList list = editedItem.FindControl("DDReferenceType") as DropDownList;

    This is because editedItem.FindControl("DDReferenceType") returns null. The defualt GridDropDownListColumnEditor for the GridDropDownColumn is of type RadComboBox (as mentioned in this help topic) and its default autogenerated id is with prefix "RCB_" that is why editedItem.FindControl("DDReferenceType") cannot find the control. You can fix this in the following way:


    RadComboBox list = editedItem["DDReferenceType"].Controls[0] as RadComboBox;

    Hope now it works.

    Kind regards,
    Marin
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. David Thompson
    David Thompson avatar
    29 posts
    Member since:
    Feb 2010

    Posted 08 Oct 2010 Link to this post

    Thank you all for your help.  I wish I could use the dropdown in display mode but I cannot since I cannot use declarative binding.  Instead, I have added the text to the query and put anoher bindcolumn on that.  If there is a way I can use the dropdown along with the binding the way I have it, I would love to know.  Thanks.
  7. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 11 Oct 2010 Link to this post

    Hi David Thompson,

    Unfortunately there is no straightforward way that allows you to use the dropdown in display mode with this kind of binding. There are various workarounds but all of them require explicit databinding of the controls, one of them is the solution that you have applied. Other possible options are:
    1. using a template column , and put the dropdown in its eidtiitem template and a standard label in the item template but you still need to bind the controls manually as before.
    2. setting directly the result of the query to the text property of the GridDataItem like this:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        
            string tableName = e.Item.OwnerTableView.Name.ToString();
            if (e.Item is GridDataItem && tableName == "Reference")
            {
                (e.Item as GridDataItem)["DDReferenceType"].Text = "";//set the 
                               //result of the query here 
            }
            if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode && tableName == "Reference"
            {
               //standard code here...
            
        }
    However this only sets the value temporarily for the currently expanded item, without persisting the result.

    Regards,
    Marin
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. David Thompson
    David Thompson avatar
    29 posts
    Member since:
    Feb 2010

    Posted 11 Oct 2010 Link to this post

    Thank you all very much for your help.  I am in the process of looking into using your ORM product as well and I was wondering if I can use that in this case to solve the problem as well.
  9. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 12 Oct 2010 Link to this post

    Hello David Thompson,

    The way you bind the data in the grid is a matter of configuration of the control itself. It is not directly connected with the way you design your business layer and how you present the data.

    Kind regards,
    Marin
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017