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

Problem with RadComboBox in GridTemplateColumn of a RadGrid

7 Answers 158 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jill-Connie Lorentsen
Top achievements
Rank 1
Jill-Connie Lorentsen asked on 07 Sep 2012, 11:44 AM

I have a RadGrid where one of the columns is a GridTemplateColumn with a RadComboBox:

<telerik:RadGrid ID="EmployeeRadGrid" runat="server" Width="96%" GridLines="None"
            AutoGenerateColumns="False" PageSize="13" AllowSorting="True" 
            AllowPaging="True" ShowStatusBar="true" Visible="False"
            OnUpdateCommand="EmployeeRadGrid_UpdateCommand" 
            OnNeedDataSource="EmployeeRadGrid_NeedDataSource"
            OnInsertCommand="EmployeeRadGrid_InsertCommand" 
            OnDeleteCommand="EmployeeRadGrid_DeleteCommand" 
            onitemdatabound="EmployeeRadGrid_ItemDataBound"            
            >
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
            <MasterTableView DataKeyNames="EmployeeID" AllowMultiColumnSorting="True" Width="100%"
                CommandItemDisplay="Top" EditMode="InPlace">
                <Columns>
                    <telerik:GridBoundColumn UniqueName="Name" SortExpression="Name" HeaderText="Navn"
                        DataField="Name" />
                    <telerik:GridTemplateColumn UniqueName="EmployeeID" HeaderText="Departement">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem, "DepartementName")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadComboBox runat="server" ID="RadComboBox1" Width="200" 
                                DataTextField="Name" DataValueField="DepartementID" DataSourceID="DepartementDataSource"
                                AllowCustomText="False" EnableAutomaticLoadOnDemand="true" OnClientLoad="OnClientLoadHandler">
                            </telerik:RadComboBox>                              
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>

The RadComboBox1 is populated through the ItemDataBound event of the EmployeeRadGrid:

protected void EmployeeRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode)
        {
             GridEditableItem item = (GridEditableItem)e.Item;
            if (!(e.Item is IGridInsertItem))
            {
                RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
                combo.Items.Clear();
  
                List<DepartementResult> deps = getDepartements();
  
                foreach (DepResult dep in deps)
                {
                    RadComboBoxItem newItem = new RadComboBoxItem();
                    newItem.Text = dep.Name;
                    newItem.Value = dep.DepartementID.ToString();
                    newItem.Attributes.Add("DepartementName", dep.Name);
  
                    combo.Items.Add(newItem);
                    newItem.DataBind();
                }
                combo.SelectedValue = ((DepartementResult)e.Item.DataItem).DepartementID.ToString();
            }
        }
    }

I also have another RadGrid, where all the departements are listed, and the user can make changes to them.

What I want is that the departements listed in RadComboBox1 are updated after changes are made in the DepartementRadGrid. Now what happens is that the RadComboBox1 is disabled in a way, - nothing happens if I click on it. The other column in the EmployeeRadGrid is editable.

If I open my page and go straight to the EmployeeRadGrid, I can access the RadComboBox1, and change the departement. But if I make changes in the EmployeeRadGrid, the RadComboBox is not responsive.

Any help is appreciated.

7 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 12 Sep 2012, 12:56 PM
Hello Jill Connie,

Please, paste your complete code-behind and mark-up - the information provided is not enough to determine the cause of the problem.

All the best,
Tsvetoslav
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.
0
Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 13 Sep 2012, 11:20 AM

Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Copy of Adm.aspx.cs" Inherits="Adm" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" 
        EnableTheming="True">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" 
                Name="Telerik.Web.UI.Common.Core.js">
            </asp:ScriptReference>
            <asp:ScriptReference Assembly="Telerik.Web.UI" 
                Name="Telerik.Web.UI.Common.jQuery.js">
            </asp:ScriptReference>
            <asp:ScriptReference Assembly="Telerik.Web.UI" 
                Name="Telerik.Web.UI.Common.jQueryInclude.js">
            </asp:ScriptReference>
        </Scripts>
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <%--<telerik:AjaxSetting AjaxControlID="DepartementComboBox">
                <UpdatedControls>                                        
                    <telerik:AjaxUpdatedControl ControlID="EmployeeTextBox" />
                    <telerik:AjaxUpdatedControl ControlID="ContactsRadGrid" />
                </UpdatedControls>
            </telerik:AjaxSetting>  --%>   
            <telerik:AjaxSetting AjaxControlID="DepartementRadGrid">
                <%--<UpdatedControls>                                                            
                    <telerik:AjaxUpdatedControl ControlID="DepartementInEmployeeGridComboBox" />                    
                </UpdatedControls>--%>
            </telerik:AjaxSetting>                                          
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadCodeBlock id="RadCodeBlock1" runat="server">
      <script type="text/javascript">
          function OnClientLoadHandler(sender) {
              sender.get_inputDomElement().readOnly = "readonly";
          }
        </script>
      
    </telerik:RadCodeBlock>
    <div>
        <asp:Label ID="Label1" runat="server" Text="Departement:" Width="100px"></asp:Label>
        <telerik:RadComboBox ID="DepartementComboBox" runat="server" 
            DataSourceID="DepartementDataSource" Skin="Outlook" Width="300px" 
            AutoPostBack="True" DataTextField="Name" DataValueField="DepartementID" 
            onselectedindexchanged="DepartementComboBox_SelectedIndexChanged">
        </telerik:RadComboBox>
        <telerik:RadButton ID="EditDepartementRadButton" runat="server" Skin="Outlook" 
            Text="Vis redigeringsverktøy..." onclick="EditDepartementRadButton_Click">
        </telerik:RadButton>
        <br />
        <br />
         
        <telerik:RadGrid ID="DepartementRadGrid" runat="server" Width="96%" GridLines="None"
            AutoGenerateColumns="False" PageSize="13" AllowSorting="True" 
            AllowPaging="True" ShowStatusBar="true" Visible="false"
  
            OnUpdateCommand="DepartementRadGrid_UpdateCommand" 
            OnNeedDataSource="DepartementRadGrid_NeedDataSource"
            OnInsertCommand="DepartementRadGrid_InsertCommand" 
            OnDeleteCommand="DepartementRadGrid_DeleteCommand"
            >
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
            <MasterTableView DataKeyNames="DepartementID" AllowMultiColumnSorting="True" Width="100%" CommandItemDisplay="Top" EditMode="InPlace">
                <Columns>
                    <telerik:GridBoundColumn UniqueName="Name" SortExpression="Name" HeaderText="Navn" DataField="Name" />
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <HeaderStyle Width="50px"></HeaderStyle></telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" Text="Delete" HeaderStyle-Width="50px" />
                      
                </Columns>
                <EditFormSettings CaptionFormatString="Endre informasjon for {0}" CaptionDataField="Name">
                    <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>
                    <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>
                    <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
                    <EditColumn ButtonType="ImageButton" />
                </EditFormSettings>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick" />
            </ClientSettings>
        </telerik:RadGrid>
          
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Employee:" Width="100px"></asp:Label>
        <telerik:RadTextBox ID="EmployeeTextBox" runat="server" 
            DataSourceID="EmployeeDataSource" Skin="Outlook" Width="300px" 
            DataTextField="Name" DataValueField="EmployeeID" ReadOnly="True">
        </telerik:RadTextBox>
        <telerik:RadButton ID="EditEmployeeRadButton" runat="server" Skin="Outlook" 
            Text="Vis redigeringsverktøy..." onclick="EditEmployeeRadButton_Click">
        </telerik:RadButton>
        <br />
        <br />
          
        <telerik:RadGrid ID="EmployeesRadGrid" runat="server" Width="96%" GridLines="None"
            AutoGenerateColumns="False" PageSize="13" AllowSorting="True" 
            AllowPaging="True" ShowStatusBar="true" Visible="False"
            OnUpdateCommand="EmployeeRadGrid_UpdateCommand" 
            OnNeedDataSource="EmployeeRadGrid_NeedDataSource"
            OnInsertCommand="EmployeeRadGrid_InsertCommand" 
            OnDeleteCommand="EmployeeRadGrid_DeleteCommand" 
            onitemdatabound="EmployeesRadGrid_ItemDataBound"            
            >
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
            <MasterTableView DataKeyNames="EmployeeID" AllowMultiColumnSorting="True" Width="100%"
                CommandItemDisplay="Top" EditMode="InPlace">                
                <Columns>
                    <telerik:GridBoundColumn UniqueName="Name" SortExpression="Name" HeaderText="Navn"
                        DataField="Name" />
                    <telerik:GridTemplateColumn UniqueName="DepartementID" HeaderText="Departement">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem, "DepartementName")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadComboBox runat="server" ID="DepartementInEmployeeGridComboBox" Width="200" SelectedValue='<%#Bind("DepartementName") %>'
                                DataTextField="Name" DataValueField="DepartementID" DataSourceID="DepartementDataSource"
                                AllowCustomText="False" Skin="Outlook" EnableAutomaticLoadOnDemand="true" OnClientLoad="OnClientLoadHandler">
                            </telerik:RadComboBox>                            
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridImageColumn >
                      
                    </telerik:GridImageColumn>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <HeaderStyle Width="50px"></HeaderStyle>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton"
                        Text="Delete" HeaderStyle-Width="50px">
                        <HeaderStyle Width="50px"></HeaderStyle>
                    </telerik:GridButtonColumn>
                </Columns>
                <EditFormSettings CaptionFormatString="Endre informasjon for {0}" CaptionDataField="Name">
                    <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>
                    <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>
                    <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
                    <EditColumn ButtonType="ImageButton" />
                </EditFormSettings>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick" />
            </ClientSettings>
  
<FilterMenu EnableImageSprites="False"></FilterMenu>
        </telerik:RadGrid>
          
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="Kontaktperson(er):" Width="134px"></asp:Label>
        <br />
          
         
        <telerik:RadGrid ID="ContactsRadGrid" runat="server" Width="96%" GridLines="None"
            AutoGenerateColumns="False" PageSize="13" AllowSorting="True" AllowPaging="True" ShowStatusBar="true"
            OnUpdateCommand="ContactsRadGrid_UpdateCommand" 
            OnNeedDataSource="ContactsRadGrid_NeedDataSource"
            OnInsertCommand="ContactsRadGrid_InsertCommand" 
            OnDeleteCommand="ContactsRadGrid_DeleteCommand" >
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
            <MasterTableView DataKeyNames="ContactID" AllowMultiColumnSorting="True" Width="100%" CommandItemDisplay="Top" EditMode="InPlace">
                <Columns>
                    <telerik:GridBoundColumn UniqueName="Name" SortExpression="Name" HeaderText="Navn" DataField="Name" />
                    <telerik:GridBoundColumn UniqueName="Title" SortExpression="Title" HeaderText="Tittel" DataField="Title" />
                    <telerik:GridBoundColumn UniqueName="TelephoneNumber" SortExpression="TelephoneNumber" HeaderText="Telefonnummer" DataField="TelephoneNumber" />
                    <telerik:GridBoundColumn UniqueName="MobilePhoneNumber" SortExpression="MobilePhoneNumber" HeaderText="Mobiltelefon" DataField="MobilePhoneNumber" />                    
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <HeaderStyle Width="50px"></HeaderStyle></telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" Text="Delete" HeaderStyle-Width="50px" />
                      
                </Columns>
                <EditFormSettings CaptionFormatString="Endre kontaktinformasjon for {0}" CaptionDataField="Name">
                    <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>
                    <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>
                    <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
                    <EditColumn ButtonType="ImageButton" />
                </EditFormSettings>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick" />
            </ClientSettings>
        </telerik:RadGrid>
          
    </div>
   
    <asp:LinqDataSource ID="DepartementDataSource" runat="server" 
        ContextTypeName="DataClassesDataContext" EntityTypeName="" Select="new (Name, DepartementID)" 
        TableName="Departements">
    </asp:LinqDataSource>
    <asp:LinqDataSource ID="EmployeeDataSource" runat="server" 
        ContextTypeName="DataClassesDataContext" EnableDelete="True" 
        EnableInsert="True" EnableUpdate="True" EntityTypeName="" 
        Select="new (EmployeeID, Name, DepartementID)" TableName="Employees">
    </asp:LinqDataSource>
    </form>
</body>
</html>

and code behind:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using VirreVapp;
  
public partial class Adm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getEmployeeDepartementID(1);
            DepartementRadGrid.DataSource = getDepartement();
            EmployeesRadGrid.DataSource = getEmployee();
            ContactsRadGrid.DataSource = getContactByDepartementID(1);            
        }
    }
  
  
    protected void DepartementComboBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {        
        int id;
  
        if (int.TryParse(e.Value, out id))
        {
            getEmployeeDepartementID(id);
            ContactsRadGrid.DataSource = getContactByDepartementID(id);//
            ContactsRadGrid.DataBind();
        }
    }
  
    private void getEmployeeDepartementID(int id)
    {
        var dataContext = new DataClassesDataContext();
        ProgramDataManager dataManager = new ProgramDataManager((SqlConnection) dataContext.Connection);
        var employee = new List<EmployeeResult>();
          
        employee = dataManager.GetEmployeeByDepartementID(id);
          
          
        RadTextBox employeeTextBox = this.FindControl("EmployeeTextBox") as RadTextBox;
        if (employeeTextBox == null) return ;
        employeeTextBox.Text = string.Empty;
  
        if (employee.Count > 0)
        {                              
            employeeTextBox.Text = employee[0].Name;
        }
    }
  
    private List<ContactResult> getContactByDepartementID(int id)
    {
        var dataContext = new DataClassesDataContext();
        ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
      
        var contacts = new List<ContactResult>();
        contacts = dataManager.GetContactByDepartementID(id);
  
        return contacts;
          
    }
  
    private List<EmployeeAndDepartementResult> getEmployee()
    {
        var dataContext = new DataClassesDataContext();
        ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
  
        var employees = new List<EmployeeAndDepartementResult>();
        employees = dataManager.GetEmployee();
  
        return employees;
    }
  
    private List<DepartementResult> getDepartement()
    {
        var dataContext = new DataClassesDataContext();
        ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
        var departement = new List<DepartementResult>();
        departement = dataManager.GetDepartement();
  
       return departement;        
    }
  
      
  
    protected void ContactsRadGrid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        string i = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ContactID"].ToString();        
  
        int id;
        if (int.TryParse(i, out id))
        {
            var dataContext = new DataClassesDataContext();
            ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
              
            try
            {
                dataManager.DeleteContact(id);
            }
            catch 
            {
                List<ContactResult> contacts = dataManager.GetContactByContactID(id);
                ContactResult contact = contacts.Find(m => m.ContactID == id);
                string warning = string.Format("{0} er registrert som kontaktperson pÃ¥ en eller flere avtaler. Vennligst slett disse før {0} kan slettes", contact.Name);
                RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(string.Format("alert('{0}')", warning));
            }            
        }
    }
      
    protected void ContactsRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
    {
        //Get the GridEditFormInsertItem of the RadGrid  
        GridEditableItem insertedItem = (GridEditableItem)e.Item;
  
        //Access the textbox from the insert form template and store the values in string variables.  
        string name = (insertedItem["Name"].Controls[0] as TextBox).Text;
        string title = (insertedItem["Title"].Controls[0] as TextBox).Text;
        string telephoneNumber = (insertedItem["TelephoneNumber"].Controls[0] as TextBox).Text;
        string mobilePhoneNumber = (insertedItem["MobilePhoneNumber"].Controls[0] as TextBox).Text;
  
        RadComboBox departementComboBox = this.FindControl("DepartementComboBox") as RadComboBox;
        if (departementComboBox == null) return;
        string i = departementComboBox.SelectedValue;
        int departementID;
        if (int.TryParse(i, out departementID))
        {
            ContactResult newContact = new ContactResult();
            newContact.Name = name;
            newContact.Title = title;
            newContact.TelephoneNumber = telephoneNumber;
            newContact.MobilePhoneNumber = mobilePhoneNumber;
            newContact.DepartementID = departementID;
  
            var dataContext = new DataClassesDataContext();
            ProgramDataManager dataManager = new ProgramDataManager((SqlConnection) dataContext.Connection);
            var contacts = new List<ContactResult>();
  
            contacts = dataManager.InsertContact(newContact);
        }
    }
  
    protected void ContactsRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadComboBox departementComboBox = this.FindControl("DepartementComboBox") as RadComboBox;
        if (departementComboBox == null) return;
  
        string i = departementComboBox.SelectedValue;
        int id;
        if (int.TryParse(i, out id))
        {
            ContactsRadGrid.DataSource = getContactByDepartementID(id);
        }
    }
  
    protected void ContactsRadGrid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        //Get the GridEditableItem of the RadGrid  
        GridEditableItem editedItem = e.Item as GridEditableItem;
        //Get the primary key value using the DataKeyValue.  
        string conID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ContactID"].ToString();
        //Access the textbox from the edit form template and store the values in string variables.  
        string name = (editedItem["Name"].Controls[0] as TextBox).Text;
        string title = (editedItem["Title"].Controls[0] as TextBox).Text;
        string telephoneNumber = (editedItem["TelephoneNumber"].Controls[0] as TextBox).Text;
        string mobilePhoneNumber = (editedItem["MobilePhoneNumber"].Controls[0] as TextBox).Text;
  
        RadComboBox departementComboBox = this.FindControl("DepartementComboBox") as RadComboBox;
        if (departementComboBox == null) return;
        string i = departementComboBox.SelectedValue;
        int departementID;
        int contactID; 
        if (int.TryParse(i, out departementID) && int.TryParse(conID, out contactID))
        {
  
            ContactResult updatedContact = new ContactResult();
            updatedContact.ContactID = contactID;
            updatedContact.Name = name;
            updatedContact.Title = title;
            updatedContact.TelephoneNumber = telephoneNumber;
            updatedContact.MobilePhoneNumber = mobilePhoneNumber;
            updatedContact.DepartementID = departementID;
  
  
            var dataContext = new DataClassesDataContext();
            ProgramDataManager dataManager = new ProgramDataManager((SqlConnection) dataContext.Connection);
            var contacts = new List<ContactResult>();
  
            contacts = dataManager.UpdateContact(updatedContact);
        }
    }
  
      
      
    protected void DepartementRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
    {
        //Get the GridEditFormInsertItem of the RadGrid  
        GridEditableItem insertedItem = (GridEditableItem)e.Item;
  
        //Access the textbox from the insert form template and store the values in string variables.  
        string name = (insertedItem["Name"].Controls[0] as TextBox).Text;
  
          
        DepartementResult newDepartement = new DepartementResult();
        newDepartement.Name = name;
  
        var dataContext = new DataClassesDataContext();
        ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
        var departement = new List<departementResult>();
  
        departement = dataManager.InsertDepartement(newDepartement);
          
    }
  
    protected void DepartementRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DepartementRadGrid.DataSource = getDepartement();
    }
  
    protected void DepartementRadGrid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        //Get the GridEditableItem of the RadGrid  
        GridEditableItem editedItem = e.Item as GridEditableItem;
        //Get the primary key value using the DataKeyValue.  
        string i = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["DepartementID"].ToString();
        //Access the textbox from the edit form template and store the values in string variables.  
        string name = (editedItem["Name"].Controls[0] as TextBox).Text;
  
          
        int departementID;
        if (int.TryParse(i, out departementID))
        {
  
            DepartementResult updatedDepartement = new DepartementResult();
            updatedDepartement.DepartementID = departementID;
            updatedDepartement.Name = name;
  
  
            var dataContext = new DataClassesDataContext();
            ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
            var departement = new List<DepartementResult>();
  
            departement = dataManager.UpdateDepartement(updatedDepartement);
              
        }
    }
      
    protected void DepartementRadGrid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        string i = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DepartementID"].ToString();
  
        int id;
        if (int.TryParse(i, out id))
        {
            var dataContext = new DataClassesDataContext();
            ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
  
            try
            {
                dataManager.DeleteDepartement(id);
            }
            catch 
            {
                List<DepartementResult> departements = dataManager.GetDepartement();
                DepartementResult departement = departements.Find(m => m.DepartementID == id);
                string warning =
                    string.Format("Det er registrert en ansatt for {0}. Vennligst slett denne før {0} kan slettes",
                                  departement.Name);
                RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(string.Format("alert('{0}')", warning));
            }
        }
    }
  
      
      
      
    protected void EmployeeRadGrid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        string i = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"].ToString();
          
  
        int id;
        if (int.TryParse(i, out id))
        {
            var dataContext = new DataClassesDataContext();
            ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
  
            try
            {
                dataManager.DeleteEmployee(id);
            }
            catch
            {
                List<EmployeeAndDepartementResult> employees = dataManager.GetEmployee();
                EmployeeAndDepartementResult employee = employees.Find(m => m.EmployeeID == id);
                string warning = string.Format( "Det er registrert avtaler pÃ¥ {0}. Vennligst slett disse før {0} kan slettes", employee.Name );
                RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(string.Format("alert('{0}')", warning ));                
            }
        }
    }
      
    protected void EmployeeRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
    {
        //Get the GridEditFormInsertItem of the RadGrid  
        GridEditableItem insertedItem = e.Item as GridEditableItem;
  
  
        Hashtable dictionary = new Hashtable();
  
        e.Item.OwnerTableView.ExtractValuesFromItem(dictionary, insertedItem);
        //Access the textbox from the insert form template and store the values in string variables.  
        string name = (insertedItem["Name"].Controls[0] as TextBox).Text;
  
        RadComboBox departementComboBox = (RadComboBox)insertedItem.FindControl("DepartementInEmployeeGridComboBox");
        if (departementComboBox == null) return;
        string i = departementComboBox.SelectedValue;
  
          
        int departementID;
        if (int.TryParse(i, out departementID))
        {
            if (!employeeExists(0, departementID))
            {
                EmployeeResult newEmployee = new EmployeeResult();
                newEmployee.Name = name;
                newEmployee.DepartementID = departementID;
  
                var dataContext = new DataClassesDataContext();
                ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
                dataManager.InsertEmployee(newEmployee);
            }
            else 
            {
                e.Canceled = true;
            }
        }
        else
        {
            e.Canceled = true;
        }
    }
  
    private bool employeeExists(int employeeID, int departementID)
    {
        var dataContext = new DataClassesDataContext();
        ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
        if (dataManager.EmployeeExistsForDepartement( employeeID, departementID) == true)
        {
            List<DepartementResult> departement = getDepartement();
            DepartementResult mres = departement.Find(m => m.DepartementID == departementID);
            string warning = string.Format("Ansatt er allerede angitt for {0}. Vennligst velg et annet departement",
                                           mres.Name);
            RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(string.Format("alert('{0}')", warning));
            return true;
        }
        return false;
    }
  
    protected void EmployeeRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        EmployeesRadGrid.DataSource = getEmployee();
    }
    protected void EmployeeRadGrid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        //Get the GridEditableItem of the RadGrid  
        GridEditableItem editedItem = e.Item as GridEditableItem;
        //Get the primary key value using the DataKeyValue.  
        string i = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"].ToString();
        //Access the textbox from the edit form template and store the values in string variables.  
        string name = (editedItem["Name"].Controls[0] as TextBox).Text;
  
        string ii = ((RadComboBox)editedItem.FindControl("DepartementInEmployeeGridComboBox")).SelectedValue;
  
  
        int employeeID;
        int departementID;
        if (int.TryParse(i, out employeeID) && int.TryParse(ii, out departementID))
        {
            if (!employeeExists(employeeID, departementID))
            {
                EmployeeResult updatedEmployee = new EmployeeResult();
                updatedEmployee.EmployeeID = employeeID;
                updatedEmployee.Name = name;
                updatedEmployee.DepartementID = departementID;
  
                var dataContext = new DataClassesDataContext();
                ProgramDataManager dataManager = new ProgramDataManager((SqlConnection)dataContext.Connection);
                var employee = new List<EmployeeResult>();
  
                employee = dataManager.UpdateEmployee(updatedEmployee);
            }
            else 
            {
                e.Canceled = true;
            }
        }
        else
        {
            e.Canceled = true;
        }        
    }
    protected void EmployeesRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
  
        if (e.Item.IsInEditMode)
        {
             GridEditableItem item = (GridEditableItem)e.Item;
  
            if (!(e.Item is IGridInsertItem))
            {
  
                RadComboBox combo = (RadComboBox)item.FindControl("DepartementInEmployeeGridComboBox");
  
                combo.Items.Clear();
  
                List<DepartementResult> departements = getDepartement();
  
                foreach (DepartementResult Departement in departements)
                {
                    RadComboBoxItem newItem = new RadComboBoxItem();
                    newItem.Text = departement.Name;
                    newItem.Value = departement.DepartementID.ToString();
                    newItem.Attributes.Add("DepartementName", departement.Name);
  
                    combo.Items.Add(newItem);
  
                    newItem.DataBind();
                }
                combo.SelectedValue = ((EmployeeAndDepartementResult)e.Item.DataItem).DepartementID.ToString();
            }
        }
    }
  
  
    protected void EditEmployeeRadButton_Click(object sender, EventArgs e)
    {
        if (!EmployeesRadGrid.Visible)
        {
            EditEmployeeRadButton.Text =  "Skjul redigeringsverktøy...";
            EmployeesRadGrid.Visible = true;
            EmployeesRadGrid.DataSource = getEmployee();
            EmployeesRadGrid.DataBind();
        }
        else
        {
            EmployeesRadGrid.Visible = false;
            EditEmployeeRadButton.Text = "Vis redigeringsverktøy...";
        }
    }
  
    protected void EditDepartementRadButton_Click(object sender, EventArgs e)
    {
        if (!DepartementRadGrid.Visible)
        {
            EditDepartementRadButton.Text = "Skjul redigeringsverktøy...";
            DepartementRadGrid.Visible = true;
            DepartementRadGrid.DataSource = getDepartement();
            DepartementRadGrid.DataBind();
        }
        else
        {
            DepartementRadGrid.Visible = false;
            EditDepartementRadButton.Text = "Vis redigeringsverktøy...";
        }
    }
     
}

Your help is appreciated!

Regards, Jill-Connie

0
Tsvetoslav
Telerik team
answered on 17 Sep 2012, 10:42 AM
Hello Jill,

Thanks for the code. I am afraid you are taking the wrong approach to that requirement. When you update the departments data source you should rebind the employees grid and then put the item that was in edit mode back into edit mode. That means that you need a way to track which item is currently being edited in the employees grid. Furthermore, I noticed that you are binding the combo both to a DataSource control and a collection of department items. You have to use either of those - I believe this is the reason why the combo becomes unresponsive.

Hope it helps.


Greetings,
Tsvetoslav
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.
0
Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 24 Sep 2012, 10:53 AM

I am not sure I understand what you mean, or if I have explained the problem correctly.

I've tried removing the combobox' dataSourceID from markup, and only binding to the list of departements, without any success.

I don't understand what you say about putting the item back in edit mode. The combo box in the EmployeesRadGrid is not neccessarily being edited when this happens. If I edit the departements, the combobox in the employees grid becomes unresponsive, and if I don't the combobox behaves as expected.

It is only the combobox that becomes unresponsive, not the other column.

Regards, Jill-Connie

0
Marin
Telerik team
answered on 27 Sep 2012, 08:45 AM
Hello Jill,

 I see that you have handled the OnClientLoad event of the combobox and you set there its input element to readonly. This might be the reason why the combo is disabled. You can remove this and instead use the AllowCustomText  to determine whether the user is allowed to write inside the textbox of the combo or not.
Also when you use automatic load on demand you should bind it using the DataSourceID property. To achieve the desired effect both the combobox and the DepartementRadGrid should be databound to the same datasource. So when you change the datasource using the grid the changes will be reflected in the combo when you click on it to open the dropdown section.

I hope this helps.

Kind regards,
Marin
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.
0
Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 28 Sep 2012, 07:45 AM

Hello again!

None of your suggestions made any difference to my problem. Binding the combobox and the DepartementsRadGrid to the same DataSourceID even made things worse, - I was no longer able to open the combobox at all.

By the way, the AllowCustomText property has no effect, - is this a bug? The user is allowed to write inside the textbox of the combobox no matter the value of the AllowCustomText.

I'm considering submitting a support ticket.

Regards, Jill-Connie Lorentsen

0
Marin
Telerik team
answered on 01 Oct 2012, 10:33 AM
Hello,

 I am attaching a sample page that shows how you can achieve this requirement. In the page you can check that when you edit a record in the Department grid and then open the combo in the Employees grid it will show the correct value. For simplicity the page only stores changes in the session and the update command is implemented only for the Department grid. Also the combo is set not to allow custom user input but it is responsive and you can pick values.

I see that you have also opened a support ticket on the same issue so in order to avoid duplicate posts we will continue any further communication there.

All the best,
Marin
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.
Tags
Grid
Asked by
Jill-Connie Lorentsen
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Jill-Connie Lorentsen
Top achievements
Rank 1
Marin
Telerik team
Share this question
or