Update From a GridViewDetailElement

4 posts, 1 answers
  1. Digger
    Digger avatar
    5 posts
    Member since:
    Sep 2009

    Posted 22 Feb 2012 Link to this post

    Hello,
    I was hoping to have the ability to Update my RadGrid from a GidViewDetailElement that was implemented as a User Control. When a user clicks on a row in the RadGrid, a separate UserControl is displayed to show the Details of that Row.  I want to have the ability for the User to change a value in one of the fields shown in the Details vVew which will ujpdate the row in the base RadGrid. The code was implemented just like your example of "Custom Views" only that i wrapped the Details view as a User Control in a RadHostItem.  I'm able to view the data just fine in the User Control, its just not apparent to me how to now pass updates from this UserControl now back to my RadGrid with this GridViewDetailElement implementation or if its even possible.  Appreciate anyone who can help on this.  Thanks - Doug

    i Have included the main pieces of code in how this was implemented.
    Here is the code in my MainForm.c file that defines the detailView GridViewDetailElement for my RadGrid rgDetails.
    private void InitializeGridControl()
            {
                this.rgDetails.TableElement.SetValue(DockLayoutPanel.DockProperty, Telerik.WinControls.Layouts.Dock.Top);
                this.rgDetails.TableElement.Margin = new Padding(10, 0, 10, 10);
                this.detailView = new GridViewDetailElement();
                this.rgDetails.GridViewElement.Panel.Children.Insert(1, this.detailView);
                this.detailView.SetValue(DockLayoutPanel.DockProperty, Telerik.WinControls.Layouts.Dock.Bottom);
                this.detailView.Margin = new Padding(10, 0, 10, 2);
            }


    Here is the Class that Defines the GridViewDetailElement (just the relevant code).
    public class GridViewDetailElement : GridVisualElement, IGridView
        {
            private RadGridViewElement gridElement;
            private GridViewInfo viewInfo;
            public DetailElement detailPanel;
      
            #region Fields
      
            private RadHostItem hostDetailView;
      
            #endregion
      
            #region Initialization
      
            protected override void InitializeFields()
            {
                base.InitializeFields();
      
                this.UseNewLayoutSystem = true;
                this.Padding = new System.Windows.Forms.Padding(10);
                this.StretchHorizontally = true;
                this.MinSize = new Size(0, 250);
                this.MaxSize = new Size(0, 250);
                this.DrawFill = true;
                this.Class = "RowFill";
                this.detailPanel = new DetailElement();
            }
      
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
                hostDetailView = new RadHostItem(detailPanel);
                this.Children.Add(hostDetailView);
            }
      
    public void Initialize(RadGridViewElement gridElement, GridViewInfo viewInfo)
            {
                this.gridElement = gridElement;
                this.viewInfo = viewInfo;
                this.gridElement.GridControl.CurrentRowChanged += new CurrentRowChangedEventHandler(GridControl_CurrentRowChanged);
            }
      
            public void Detach()
            {
                this.gridElement.GridControl.CurrentRowChanged -= new CurrentRowChangedEventHandler(GridControl_CurrentRowChanged);
                this.gridElement = null;
                this.viewInfo = null;
            }
      
    public void UpdateView()
            {
                GridViewDataRowInfo dataRow = this.GridViewElement.GridControl.CurrentRow as GridViewDataRowInfo;
      
                if (dataRow != null)
                {
                    detailPanel.UpdateUCView(dataRow);
                }
            }
      
            public RadGridViewElement GridViewElement
            {
                get { return this.gridElement; }
            }
      
            public GridViewInfo ViewInfo
            {
                get { return this.viewInfo; }
            }
      
            #endregion
      
            #region Event Handlers
      
            private void GridControl_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
            {
                this.UpdateView();
            }
      
            #endregion
         ...
    }

    And Here is my UserControl Code (relevant code only)

    public partial class DetailElement : UserControl
        {
            public DetailElement()
            {
                InitializeComponent();
            }
     
            public void UpdateUCView(GridViewDataRowInfo dataRow)
            {
                string statusView;
                if (dataRow != null)
                {               
                    statusView = GetSafeString(dataRow.Cells["State"].Value.ToString());
                    switch (statusView)
                    {
                        case "Running":
                            picBoxStatus.Image = Properties.Resources.start24;
                            break;
                        case "Paused":
                            picBoxStatus.Image = Properties.Resources.pause24;
                            break;
                        case "Stopped":
                            picBoxStatus.Image = Properties.Resources.stop_red24;
                            break;
                        default:
                            picBoxStatus.Image = Properties.Resources.warning16;
                            break;
                    } 
                    this.rTxtServiceOwner.Text = GetSafeString(dataRow.Cells["StartName"].Value.ToString());
                    this.rTxtServiceType.Text = GetSafeString(dataRow.Cells["ServiceType"].Value.ToString());
                    ....
             }
     
    private void rChkTrackService_Click(object sender, EventArgs e)
            {
            // How Do I pass an update from this CheckBox back to my main Grid ???????????????
            }
    }

    Thanks Doug


  2. Answer
    Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 24 Feb 2012 Link to this post

    Hi Doug,

    Thank you for writing and for the provided code.

    Since your user control is always displaying the information from the current row, you can keep the current row in a field in your user control and when you (un)check the check box, you can directly edit the row info which will be reflected in the grid view. I have attached a sample project where I have used your code with some modification to make it work and demonstrate the editing. 

    I hope this will be useful for you. Should you have further questions, do not hesitate to write back.
     
    Kind regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Digger
    Digger avatar
    5 posts
    Member since:
    Sep 2009

    Posted 24 Feb 2012 Link to this post

    Ivan,
    Its always the simple solutions that seem to elude me. By just persisting the datarow that's passed in for updating my UserContorl in a global variable, i can use this same instance to update that row in the underlying Grid.
    Thanks so much Ivan, Telerik controls and support is the best in the industry.. You guys rock....

    Thanks
    Doug
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 29 Feb 2012 Link to this post

    Hi Doug,

    Thank you for your reply and for the kind words.

    I am happy I have been able to help you out. Feel free to contact us back should you have other questions.

    Greetings,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top