How to Enable a Checkbox in ChildRows

3 posts, 0 answers
  1. Meaning Of Lights
    Meaning Of Lights avatar
    24 posts
    Member since:
    Dec 2007

    Posted 23 Jul 2013 Link to this post

    I've trawled the web and have exhausted troubleshooting. Despite others asking very similar questions nothing seems to allow me edit a Checkbox in a Template Column.

    I've attached a screenshot and what I'm doing is possible as its shown here: http://www.telerik.com/help/winforms/gridview-hierarchical-grid-tutorial-binding-to-hierarchical-data.html

      protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                this.radGridView1.BeginUpdate();
                LoadData();
                this.radGridView1.UseScrollbarsInHierarchy = true;
                this.radGridView1.AutoExpandGroups = true;
                this.radGridView1.ShowGroupPanel = false;
                this.radGridView1.AllowAddNewRow = false;
                this.radGridView1.EndUpdate();
            }

     private void LoadData()
            {
                latestRevisions = DAL.GetTemplatesLatestRevisionsBySearchCrtieria(...);
                pastRevisions = DAL.GetTemplatesPastRevisionsBySearchCrtieria(...);

                latestRevisionsBindingSource.DataSource = latestRevisions;
                pastRevisionsBindingSource.DataSource = pastRevisions;

                radGridView1.DataSource = latestRevisionsBindingSource;

                radGridView1.MasterGridViewTemplate.Columns["Name"].Width = 150; 
                radGridView1.MasterGridViewTemplate.Columns["TemplateID"].IsVisible = false;
                radGridView1.MasterGridViewTemplate.Columns["ParentTemplateID"].IsVisible = false;
                radGridView1.MasterGridViewTemplate.Columns["Description"].Width = radGridView1.Width - twoHundred;
                radGridView1.MasterGridViewTemplate.Columns["ImagePreview"].IsVisible = false;

                foreach (GridViewDataColumn column in this.radGridView1.Columns)
                {
                    column.ReadOnly = (column.Name != "Compare");
                }

                LoadDetailsTable();
            }

            void LoadDetailsTable()
            {
                GridViewTemplate template = new GridViewTemplate();
                template.Caption = "Latest Revisions";
                template.DataSource = pastRevisions;
                template.AllowRowResize = false;
                template.ShowColumnHeaders = false;
                template.Columns["ImagePreview"].Width = 100;
                template.ReadOnly = true;

                template.Columns["Description"].Width = 245;
                template.Columns["Description"].DisableHTMLRendering = false;

                template.Columns["Compare"].Width = 60;
                template.Columns["Compare"].ReadOnly = false;   //<-- Read-Only is false!!!

                this.radGridView1.Templates.Insert(0, template);
                
                GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate);
                relation.ChildTemplate = template;
                relation.ParentColumnNames.Add("TemplateID");
                relation.ChildColumnNames.Add("ParentTemplateID");
                this.radGridView1.Relations.Add(relation);

                HtmlViewDefinition viewDef = new HtmlViewDefinition();
                viewDef.RowTemplate.Rows.Add(new RowDefinition());
                viewDef.RowTemplate.Rows.Add(new RowDefinition());

                viewDef.RowTemplate.Rows[0].Cells.Add(new CellDefinition("ImagePreview", 0, 1, 2));
                viewDef.RowTemplate.Rows[0].Cells.Add(new CellDefinition("Description", 0, 1, 2));
                viewDef.RowTemplate.Rows[0].Cells.Add(new CellDefinition("Compare", 0, 1, 2));
                template.ViewDefinition = viewDef;
            }

    private void radGridView1_SelectionChanged(object sender, EventArgs e)
            {
                if (prevRow != null)
                    prevRow.Cells["Compare"].Value = false;
                prevRow = this.radGridView1.SelectedRows[0];
                this.radGridView1.Columns["Compare"].ReadOnly = false;
                this.radGridView1.SelectedRows[0].Cells["Compare"].ReadOnly = false;
                this.radGridView1.SelectedRows[0].Cells["Compare"].Value = true;         /<- Throws an Error "Column 'Compare' is read only.!!!
            }

    If anyone could tell me how to fix this it would be much appreciated. I've spent two days on this now.
  2. Meaning Of Lights
    Meaning Of Lights avatar
    24 posts
    Member since:
    Dec 2007

    Posted 25 Jul 2013 Link to this post

    I found the problem.

    In my SQL I was adding a bit column called Compare

    SELECT TemplateID,ParentTemplateID,Name, [Description], ImagePreview,0 as Compare....

    Here is a repro of the successful implementation:
    public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
     
          protected override void OnLoad(EventArgs e)
          {
              base.OnLoad(e);
     
              this.radGridView1.BeginUpdate();
     
              LoadData();
     
              this.radGridView1.UseScrollbarsInHierarchy = true;
              this.radGridView1.ReadOnly = false;
              this.radGridView1.AutoExpandGroups = true;
              this.radGridView1.ShowGroupPanel = false;
              this.radGridView1.AllowAddNewRow = false;
     
              this.radGridView1.EndUpdate();
          }
     
          private void LoadData()
          {
              DataTable dtParent = new DataTable();
              DataTable dtChild = new DataTable();
     
              dtParent.Columns.Add("TemplateID", typeof(int));
              dtParent.Columns.Add("ParentTemplateID", typeof(int));
              dtParent.Columns.Add("Name");
              dtParent.Columns.Add("Description");
              dtParent.Columns.Add("ImagePreview",typeof(Image));
              dtParent.Columns.Add("Compare", typeof(bool));
     
              DataRow dr = dtParent.NewRow();
              dr["TemplateID"] = 43;
              dr["ParentTemplateID"] = 0;
              dr["Name"] = "The Name";
              dr["Description"]= "The Description";
     
              dtParent.Rows.Add(dr);
     
     
              dtChild.Columns.Add("TemplateID", typeof(int));
              dtChild.Columns.Add("ParentTemplateID", typeof(int));
              dtChild.Columns.Add("Name");
              dtChild.Columns.Add("Description");
              dtChild.Columns.Add("ImagePreview", typeof(Image));
              dtChild.Columns.Add("Compare", typeof(bool));
     
              DataRow dr1 = dtChild.NewRow();
              dr1["TemplateID"] = 44;
              dr1["ParentTemplateID"] = 43;
              dr1["Name"] = "Testing 1";
              dr1["Description"] = "Testing 1";
              dr1["ImagePreview"] = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
              dr1["Compare"] = false;
              dtChild.Rows.Add(dr1);
     
              dr1 = dtChild.NewRow();
              dr1["TemplateID"] = 45;
              dr1["ParentTemplateID"] = 43;
              dr1["Name"] = "Testing 2";
              dr1["Description"] = "Testing 2";
              dr1["ImagePreview"] = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
              dr1["Compare"] = false;
              dtChild.Rows.Add(dr1);
     
              using (this.radGridView1.DeferRefresh())
              {
                  this.radGridView1.MasterTemplate.Reset();
     
                  radGridView1.DataSource = dtParent;
     
                  radGridView1.MasterGridViewTemplate.Columns["Name"].Width = 150;
                  radGridView1.MasterGridViewTemplate.Columns["Description"].Width = 650;
                  radGridView1.MasterGridViewTemplate.Columns["TemplateID"].IsVisible = false;
                  radGridView1.MasterGridViewTemplate.Columns["ParentTemplateID"].IsVisible = false;
                  radGridView1.MasterGridViewTemplate.Columns["Compare"].IsVisible = false;
                  radGridView1.MasterGridViewTemplate.Columns["ImagePreview"].IsVisible = false;
     
                  foreach (GridViewDataColumn column in this.radGridView1.Columns)
                  {
                      column.ReadOnly = (column.Name != "Compare");
                  }              
              }
              LoadDetailsTable(dtChild);
          }     
     
          private  void LoadDetailsTable(DataTable dtchild)
          {
              GridViewTemplate template = new GridViewTemplate();
              template.Caption = "Latest Revisions";
              template.DataSource = dtchild;
              template.AllowRowResize = false;
              template.ShowColumnHeaders = false;
              template.Columns["ImagePreview"].Width = 100;
              template.AllowAddNewRow = false;
     
              template.Columns["Description"].Width = 245;
              template.Columns["Description"].ReadOnly = true;
              template.Columns["Description"].DisableHTMLRendering = false;
              template.Columns["Description"].WrapText = true;
     
              template.Columns["Compare"].Width = 60;
              template.Columns["Compare"].ReadOnly = false;
     
              this.radGridView1.Templates.Insert(0, template);
     
              GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate, template);
              relation.ChildTemplate = template;
              relation.ParentColumnNames.Add("TemplateID");
              relation.ChildColumnNames.Add("ParentTemplateID");
              this.radGridView1.Relations.Add(relation);
     
              HtmlViewDefinition viewDef = new HtmlViewDefinition();
              viewDef.RowTemplate.Rows.Add(new RowDefinition());
     
              viewDef.RowTemplate.Rows[0].Cells.Add(new CellDefinition("ImagePreview", 0, 1, 1));
              viewDef.RowTemplate.Rows[0].Cells.Add(new CellDefinition("Description", 0, 1, 1));
              viewDef.RowTemplate.Rows[0].Cells.Add(new CellDefinition("Compare", 0, 1,1));
              template.ViewDefinition = viewDef;
     
              SetupFiltering(radGridView1.MasterTemplate, true);
          }
     
          private void SetupFiltering(GridViewTemplate template, bool enableFiltering)
          {
              template.EnableFiltering = enableFiltering;
     
              for (int i = 0; i < template.Templates.Count; i++)
              {
                  SetupFiltering(template.Templates[i], enableFiltering);
              }
          }
          private void radGridView1_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
          {
              for (int j = 0; j < e.ChildViewInfo.Rows.Count; j++)
              {
                  e.ChildViewInfo.Rows[j].Height = 100;
              }
          }
      }
  3. UI for WinForms is Visual Studio 2017 Ready
  4. George
    Admin
    George avatar
    500 posts

    Posted 26 Jul 2013 Link to this post

    Hello Jeremy,

    Thank you for sharing your solution with the community, I am sure someone will find it helpful.

    If you have any other questions or comments, please let me know.
     
    Regards,
    George
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top