creatin dynamic column with radcomboboxes in rad grid

3 posts, 0 answers
  1. anirud
    anirud avatar
    9 posts
    Member since:
    Mar 2011

    Posted 09 Mar 2011 Link to this post

    //calling this function inside an event
        public void loadGrid()
        {
          //looping for creating 31 columns
                for (int I_ColumnCount = 1; I_ColumnCount <= 31; I_ColumnCount++)
                {
                      
                    GridTemplateColumn gridtemplatecolumn = new GridTemplateColumn();
                    gridtemplatecolumn.ItemTemplate = new MyTemplate(Convert.ToString(I_ColumnCount));
                    gridtemplatecolumn.HeaderText = Convert.ToString(I_ColumnCount);
                    this.rg_Attendence.MasterTableView.Columns.Add(gridtemplatecolumn);
                    ViewState["grid_DayCount"]=Convert.ToString(I_ColumnCount);
                }
        }
         
        public class MyTemplate : ITemplate
        {
            protected RadComboBox lblControl;
            public string colname;
            public MyTemplate(string cName)
            {
                colname = cName;
                  
            }
            public void InstantiateIn(System.Web.UI.Control container)
            {
                  
                lblControl = new RadComboBox();
                lblControl.ID = colname;
                container.Controls.Add(lblControl);
            }
        }
      
        //this function contains data which is retrieved from database and need to bind to rad grid and comboboxes(which are created inside rad grid dynamically)
        protected void loadEmployees()
        {
                RadComboBox lblControl = new RadComboBox();
                 
                DataTable DT_Employee = new DataTable();
                DT_Employee = BLL.get_Attendance(_obj_Smhr_Attendance);
                if (DT_Employee.Rows.Count == 0)
                {
                     
                        rg_Attendence.Visible = true;
                        rg_Attendence.DataSource = DT_Employee;
                        rg_Attendence.DataBind();
                        for (i = 0; i <= rg_Attendence.Items.Count - 1; i++)
                        {
                            //here i am trying to bind data to comboboxes which are created dynamically inside rad grid
                            rcmbList = rg_Attendence.Items[i].FindControl("lblControl") as RadComboBox;
                            if (Convert.ToString(Convert.ToString(DT_Employee.Rows[i][3])) == "0")
                                rcmbList.SelectedIndex = rcmbList.FindItemIndexByValue(Convert.ToString("P"));
                            else if (Convert.ToString(Convert.ToString(DT_Employee.Rows[i][3])) == "1")
                                rcmbList.SelectedIndex = rcmbList.FindItemIndexByValue(Convert.ToString("L"));
                             
                        }
                    }
                    else
                    {
                        rg_Attendence.Visible = false;
                        btn_Save.Visible = false;
                        btn_Finalize.Visible = false;
                    }
                }
                else
                {
                    _obj_Smhr_Attendance.OPERATION = operation.Check;
                    _obj_Smhr_Attendance.ATTENDANCE_MODE = true;
                    DT_Employee = BLL.get_Attendance(_obj_Smhr_Attendance);
                    rg_Attendence.Visible = true;
                    rg_Attendence.DataSource = DT_Employee;
                    rg_Attendence.DataBind();
                    for (i = 0; i <= rg_Attendence.Items.Count - 1; i++)
                    {
                        //here i am trying to bind data to comboboxes which are created dynamically inside rad grid
                        lblControl = rg_Attendence.Items[i].FindControl("colname") as RadComboBox;
                        if ((Convert.ToString(Convert.ToString(DT_Employee.Rows[i][2]))).Trim() == "P")
                        {
                            lblControl.SelectedIndex = lblControl.FindItemIndexByValue(Convert.ToString("P"));
                        }
                        else if ((Convert.ToString(Convert.ToString(DT_Employee.Rows[i][2]))).Trim() == "L")
                        {
                            lblControl.SelectedIndex = lblControl.FindItemIndexByValue(Convert.ToString("L"));
                        }
                         
                          
                    }
                     
                }
                 
        }
    }
    on the index change of rad combobox arad grid must appear with dynamically created columns containing rad comboboxes and those comboboxes are to be binded with some data from database.the columns with comboboxes are coming but i am unable to bind with data. i am doing this programmatically using ITEMPLATE interface by inheriting in a class and calling InstantiateIn method.
    help by sending or posting a code snippet.
    thanks in advance
    Anirud
  2. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1268 posts

    Posted 15 Mar 2011 Link to this post

    Hello Anirud,

    From the code snippet provided it seems that no data source is defined for this combobox, and you don't call its databind method. You could check this demo here, where a combobox is data bound and situated in a RadGrid. Despite the fact that your combobox is dynamically created, you could follow the approach used for data binding.

    Regards,
    Dimitar Terziev
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. anirud
    anirud avatar
    9 posts
    Member since:
    Mar 2011

    Posted 16 Mar 2011 Link to this post

    Hello Dimitar Terziev ,
    Thanks for your response. I tried to bind the comboboxes in the way you suggested but the combobox at the last columns are only getting binded others are not.
    public void loadGrid()
       {
               DataTable dt_NoOfDays = Dal.ExecuteQuery("SELECT DATEDIFF(DD,PRDDTL_STARTDATE,PRDDTL_ENDDATE) as NoOfDays FROM SMHR_PERIODDETAILS WHERE PRDDTL_ID='"+Convert.ToInt32(rcmb_AttPeriodElement.SelectedValue)+"'");
               int I_Days = Convert.ToInt32(dt_NoOfDays.Rows[0][0]);
               // here i am creating columns
               for (int I_ColumnCount = 1; I_ColumnCount <= I_Days; I_ColumnCount++)
               {
                   GridTemplateColumn gridtemplatecolumn = new GridTemplateColumn();
                   gridtemplatecolumn.ItemTemplate = new MyTemplate(Convert.ToString(I_ColumnCount));
                   gridtemplatecolumn.DataField = Convert.ToString("LEAVE_STATUS");
                   gridtemplatecolumn.HeaderText = Convert.ToString(I_ColumnCount);
                   gridtemplatecolumn.UniqueName = Convert.ToString(I_ColumnCount);
                   this.rg_Attendence.MasterTableView.Columns.Add(gridtemplatecolumn);
                   ViewState["grid_DayCount"]=Convert.ToString(I_ColumnCount);
                   string grid_DayCount = Convert.ToString(ViewState["grid_DayCount"]);
                   //rcmbList = rg_Attendence.Items[I_ColumnCount].FindControl("rcmb_Att_Status") as RadComboBox;
                   loadEmployees();
               }
       }
     
       protected void rg_Attendence_ItemDataBound(object sender, GridItemEventArgs e)
       {
            if (e.Item is GridDataItem)
           {
               GridDataItem item = e.Item as GridDataItem;
               string str = Convert.ToString(ViewState["grid_DayCount"]);
               RadComboBox comboBox1 = (RadComboBox)item[Convert.ToString(ViewState["grid_DayCount"])].FindControl(Convert.ToString(ViewState["grid_DayCount"]));
               DataTable Attendance_Status = (DataTable)ViewState["Attendance_Status"];
               comboBox1.DataSource = Attendance_Status;
               comboBox1.DataTextField = "LEAVE_STATUS";
               comboBox1.DataValueField = "LEAVE_STATUS";
               comboBox1.DataBind();
             
               for (int I_DataTableCount = 0; I_DataTableCount <= Attendance_Status.Rows.Count - 1;I_DataTableCount++ )
               {
                   string str21 = Convert.ToString(Attendance_Status.Rows[I_DataTableCount]["LEAVE_STATUS"]).Trim();
                   int index = comboBox1.FindItemIndexByValue(Convert.ToString(Attendance_Status.Rows[I_DataTableCount]["LEAVE_STATUS"]).Trim());
                   comboBox1.SelectedIndex = comboBox1.FindItemIndexByValue(Convert.ToString(Attendance_Status.Rows[I_DataTableCount]["LEAVE_STATUS"]).Trim());
               }
                
               if (Attendance_Status.Rows[0][3].ToString() == "1")
               {
                   rcmbList.Enabled = false;
               }
       }
        
       public class MyTemplate : ITemplate
       {
           protected RadComboBox lblControl;
           public string colname;
           public MyTemplate(string cName)
           {
               colname = cName;
                
           }
     
           public void InstantiateIn(System.Web.UI.Control container)
           {
     
               lblControl = new RadComboBox();
               lblControl.ID = colname;
               lblControl.AutoPostBack = true;
               container.Controls.Add(lblControl);
               lblControl.Items.Insert(0, new RadComboBoxItem("Select", "Select"));
               lblControl.Items.Insert(1, new RadComboBoxItem("P", "P"));
               lblControl.Items.Insert(2, new RadComboBoxItem("A", "A"));
               lblControl.Items.Insert(3, new RadComboBoxItem("L", "L"));
               lblControl.Items.Insert(4, new RadComboBoxItem("W", "W"));
               lblControl.Items.Insert(5, new RadComboBoxItem("T", "T"));
               lblControl.Items.Insert(6, new RadComboBoxItem("C", "C"));
               lblControl.Items.Insert(7, new RadComboBoxItem("H", "H"));
               lblControl.Items.Insert(8, new RadComboBoxItem("HD", "HD"));
           }
       }
     
       protected void loadEmployees()
       {
           
               int i = 0;
               string str_PeriodElementMonth = null;
               string str_PeriodYear = null;
               SMHR_PERIOD _obj_smhr_Period = new SMHR_PERIOD();
               _obj_smhr_Period.OPERATION = operation.Check1;
               _obj_smhr_Period.PERIOD_ID = Convert.ToInt32(rcmb_AttPeriod.SelectedValue);
               DataTable dt_Period = BLL.get_PeriodHeaderDetails(_obj_smhr_Period);
               if (dt_Period.Rows.Count != 0)
               {
                   str_PeriodYear = Convert.ToString(dt_Period.Rows[0][0]);
               }
     
               SMHR_PERIODDTL _obj_smhr_PeriodDetails = new SMHR_PERIODDTL();
               _obj_smhr_PeriodDetails.OPERATION = operation.Check1;
               _obj_smhr_PeriodDetails.PRDDTL_ID = Convert.ToInt32(rcmb_AttPeriodElement.SelectedValue);
               _obj_smhr_PeriodDetails.PRDDTL_PERIOD_ID = Convert.ToInt32(rcmb_AttPeriod.SelectedValue);
               DataTable dt_PeriodDetails = BLL.get_PeriodDetails(_obj_smhr_PeriodDetails);
               if (dt_PeriodDetails.Rows.Count != 0)
               {
                   str_PeriodElementMonth = Convert.ToString(dt_PeriodDetails.Rows[0][0]);
               }
               string str_DayCount = Convert.ToString(ViewState["grid_DayCount"]);
               string str_Attendance = str_PeriodElementMonth + "/" + str_DayCount + "/" + str_PeriodYear + " " + Convert.ToString(DateTime.Now.ToLongTimeString());
     
               SMHR_ATTENDANCE _obj_Smhr_Attendance = new SMHR_ATTENDANCE();
               _obj_Smhr_Attendance.OPERATION = operation.Check;
               _obj_Smhr_Attendance.ATTENDANCE_MODE = true;
               _obj_Smhr_Attendance.ATTENDANCE_BU_ID = Convert.ToInt32(rcmb_AttBusinessUnit.SelectedItem.Value);
               _obj_Smhr_Attendance.ATTENDANCE_DATE = Convert.ToDateTime(str_Attendance);
               DataTable DT_Employee = new DataTable();
               DT_Employee = BLL.get_Attendance(_obj_Smhr_Attendance);
                   rg_Attendence.Visible = true;
                   rg_Attendence.DataSource = DT_Employee;
                   rg_Attendence.DataBind();
                   for (i = 0; i <= rg_Attendence.Items.Count - 1; i++)
                   {
                       rcmbList = rg_Attendence.Items[i].FindControl(Convert.ToString(ViewState["grid_DayCount"])) as RadComboBox;
     
                       if ((Convert.ToString(Convert.ToString(DT_Employee.Rows[i][2]))).Trim() == "P")
                       {
                           rcmbList.SelectedIndex = rcmbList.FindItemIndexByValue(Convert.ToString("P"));
                       }
                       else if ((Convert.ToString(Convert.ToString(DT_Employee.Rows[i][2]))).Trim() == "A")
                       {
                           rcmbList.SelectedIndex = rcmbList.FindItemIndexByValue(Convert.ToString("A"));
                       }
                        
                       if (DT_Employee.Rows[0][3].ToString() == "1")
                       {
                           rcmbList.Enabled = false;
                       }
                   }
                    
               }
                 
               dt_ds = DT_Employee;
           }
           
       }

    Below is my code.

    Thanks
Back to Top