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

[Solved] Selected Value is not changing in RadComboBox which is in RadGrid

1 Answer 332 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leadeng Leadeng
Top achievements
Rank 1
Leadeng Leadeng asked on 26 Nov 2009, 05:17 AM
Dear Telerik
                       I placed a radcombobox inside a radgrid.I have autopostback set to true. I am using a GridTemplateColumn inside that I have filtertemplate inside that I placed a radcombobox .I also have itemtemplate inside that I have a checkbox.The combobox is populated with three values attended,unattended and all.when I select unattended/all in radcombobox it is showing the selectedvalue as attended only after postback.If attended is selected in radcombobox then the check boxes appears as checked.If unattended is selected from radcombobox then the checkboxes appears as unchecked.Please let me know what is wrong here.urgent please...

Here is the lines of code of importance

<telerik:GridTemplateColumn ShowFilterIcon="false" HeaderText="Attendance"  CurrentFilterFunction="EqualTo">
    <FilterTemplate>
    <telerik:RadComboBox ID="cmbAttend" runat="server"  DataTextField="ATTENDANCE_TYPE_DESC" DataValueField="ATTENDANCE_TYPE_ID"
    EnableViewState="true"  AutoPostBack="true">
    </telerik:RadComboBox>
    </FilterTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkAttend" runat="server" />
    </ItemTemplate>
    </telerik:GridTemplateColumn>


aspx code


<telerik:RadGrid ID="gvEventHistory" skin ="Default" runat="server" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true"  PageSize="100"
     AllowFilteringByColumn="true" OnItemDataBound="gvEventHistory_ItemDataBound" OnItemCreated="gvEventHistory_ItemCreated"      OnNeedDataSource="gvEventHistory_NeedDataSource">
    <GroupingSettings CaseSensitive="false" />
    <MasterTableView DataKeyNames="SCHEDULE_EVENT_ID" ClientDataKeyNames="SCHEDULE_EVENT_ID" AllowFilteringByColumn="True">
    <RowIndicatorColumn>
    <HeaderStyle Width="2px" />
    <ItemStyle Width ="10px" />
    </RowIndicatorColumn>
   <Columns>
<telerik:GridTemplateColumn ShowFilterIcon="false" HeaderText="Attendance"  CurrentFilterFunction="EqualTo">
    <FilterTemplate>
    <telerik:RadComboBox ID="cmbAttend" runat="server"  DataTextField="ATTENDANCE_TYPE_DESC" DataValueField="ATTENDANCE_TYPE_ID"
    EnableViewState="true"  AutoPostBack="true">
    </telerik:RadComboBox>
    </FilterTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkAttend" runat="server" />
    </ItemTemplate>
    </telerik:GridTemplateColumn>
   </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true">
    </ClientSettings>
    </telerik:RadGrid>

C# code

protected void Page_Load(object sender, EventArgs e)
    {

        aEmpNo = Convert.ToInt32(Request.QueryString["empno"]);
        if (!Page.IsPostBack)
        {
            //Load all events default view
            m_EmpEventHistoryList = EmployeeEventHistoryManager.Instance.GetEventHistory(aEmpNo );
            gvEventHistory.DataSource = m_EmpEventHistoryList;
            gvEventHistory.DataBind();

        }
    }



protected void gvEventHistory_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        m_EmpEventHistoryList = EmployeeEventHistoryManager.Instance.GetEventHistory(this.EmployeeNoParameter);
        gvEventHistory.DataSource = m_EmpEventHistoryList;
    }
   

    protected void gvEventHistory_ItemDataBound(object sender, GridItemEventArgs e)
    {
        RadComboBox cmbEventType = null;
        RadComboBox cmbAttend = null;
        string aAttendValue = string.Empty;
        int aIndex = 0;
        string SelectedValue = "1";

        //Get the eventtypedesc from event_type table
        //GetScheduleEvent will bring all the events and bind it to combobox
        IList<ScheduleEvent> m_ScheduleList = ScheduleEventManager.Instance.GetScheduleEvent();

        //Get this list and bind them to cmbattend combobox which contains attended,unattended
        //all rows
        IList<EmployeeEventHistory> m_EmployeeEventAttendedList = EmployeeEventHistoryManager.Instance.GetEventAttendedDetails();

        if (e.Item is GridFilteringItem)
        {
              cmbAttend = (RadComboBox)e.Item.FindControl("cmbAttend");

             cmbAttend.DataSource = m_EmployeeEventAttendedList;
            cmbAttend.DataBind();
             

        }

}

protected void gvEventHistory_ItemCreated(object sender, GridItemEventArgs e)
    {
        RadComboBox cmbEventType = null;
        RadComboBox cmbAttend = null;
        if (e.Item is GridFilteringItem)
        {
             //Get the cmbAttend RadComboBox
            cmbAttend = (RadComboBox)e.Item.FindControl("cmbAttend");
            cmbAttend.ID = "cmbAttend";
            cmbAttend.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cmbAttend_SelectedIndexChanged);
        }
    }



void cmbAttend_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        IList<WW.FS.SPSS.Business.Employee.EmployeeEventHistory> m_EmpEventHistoryList = null;
        string All = "All";

        int Attended = 1;
        int UnAttended = 2;

        RadComboBox cmbAttend = (RadComboBox)o;
        int aAttendTypeId = 0;
        int aCounter = 0;

        //If All is selected then this code block must be executed
        if (cmbAttend.Text.ToString().ToUpper().Trim() == All.ToUpper().Trim())
        {
            aAttendTypeId = Convert.ToInt32(cmbAttend.SelectedValue.ToString());
            m_EmpEventHistoryList = EmployeeEventHistoryManager.Instance.GetEmpEventAttendance(aAttendTypeId);
            foreach (GridDataItem aGridItem in gvEventHistory.Items)
            {
                if (aGridItem.ItemType == GridItemType.Item)
                {
                    CheckBox chkAttended = (CheckBox)aGridItem.FindControl("chkAttend");
                    if (chkAttended != null)
                    {
                            if (Convert.ToString(m_EmpEventHistoryList[aCounter].ATTENDANCE_TYPE_ID).ToUpper().Trim() == Convert.ToString(Attended).ToUpper().Trim())
                            {
                                chkAttended.Checked = true;

                            }
                            else
                                if (Convert.ToString(m_EmpEventHistoryList[aCounter].ATTENDANCE_TYPE_ID).ToUpper().Trim() == Convert.ToString(UnAttended).ToUpper().Trim())
                                {
                                    chkAttended.Checked = false;

                                }
                       
                         }
                }

            }

            gvEventHistory.DataSource = m_EmpEventHistoryList;
            gvEventHistory.DataBind();





Thanks and Regards
Sri

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 01 Dec 2009, 12:05 PM
Hello Leadeng,

I noticed in the code, that you are setting the datasource for the grid directly, and calling DataBind().
Please, keep in mind, that this is not a recommended approach. You can use the NeedDataSource event handler to pass data to the control, as shown in the following article:

http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

Calling databind directly at any other stage of the page lifecycle will break the default functionalities of the control.
I hope these suggestions get you started properly.

Best wishes,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Leadeng Leadeng
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or