Add custom row programatically

4 posts, 0 answers
  1. Simon
    Simon avatar
    32 posts
    Member since:
    Aug 2011

    Posted 20 Dec 2016 Link to this post

    Hi

    I'd like to add custom rows that have a single button on them. I've managed to get a custom row with a button showing instead of the new row as per your documentation, but I'd like to get it showing elsewhere. My grid is not data bound - I'm programatically adding rows to it and I'd like to programatically add the custom row at certain positions. My grid includes a single level of grouping, so I'd need to be able to add the row to a specified group. I'd also like to be able to use different button text in some cases and to pass the button click event up to the form that the grid is on. How do I do this?

    Thanks

  2. Dimitar
    Admin
    Dimitar avatar
    2951 posts

    Posted 21 Dec 2016 Link to this post

    Hello Simon,

    Thank you for writing.

    I have created an example for this:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            radGridView1.CreateRow += RadGridView1_CreateRow;
            radGridView1.Columns.Add("Col1");
            radGridView1.Columns.Add("Col2");
            radGridView1.Columns.Add("Col3");
     
            for (int i = 0; i < 100; i++)
            {
                radGridView1.Rows.Add("Row" + i, "test", "test");
            }
     
            radGridView1.Rows[5].Tag = "button";
            radGridView1.Rows[9].Tag = "button";
     
            radGridView1.RowFormatting += RadGridView1_RowFormatting;
        }
     
        private void RadGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            MyDataRow row = e.RowElement as MyDataRow;
            if (row != null && row.Button.Visibility == ElementVisibility.Visible)
            {
                row.Button.Text = "Test";
            }
        }
     
        public void ButtonClicked()
        {
            RadMessageBox.Show("Clicked");
        }
        private void RadGridView1_CreateRow(object sender, GridViewCreateRowEventArgs e)
        {
            if (e.RowType == typeof(GridDataRowElement) )
            {
                e.RowElement = new MyDataRow();
            }
        }
     
        private void radButton1_Click(object sender, EventArgs e)
        {
     
        }
    }
    class MyDataRow : GridDataRowElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridDataRowElement);
            }
        }
     
        public RadButtonElement Button
        {
            get
            {
                return button;
            }
     
            
        }
     
        RadButtonElement button = new RadButtonElement();
        protected override void CallCreateChildElements()
        {
             base.CallCreateChildElements();
     
            Button.StretchHorizontally = true;
            Button.StretchVertically = true;
            Button.Text = "test";
            Button.ZIndex = 100;
            Button.Click += Button_Click;
            this.Children.Add(Button);
        }
     
        private void Button_Click(object sender, EventArgs e)
        {
            var form = this.GridControl.Parent as RadForm1;
            form.ButtonClicked();
        }
     
        public override GridCellElement CreateCell(GridViewColumn column)
        {
            var cell = base.CreateCell(column);
           
            return cell;
     
        }  
     
        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            var result = base.ArrangeOverride(finalSize);
            if (this.RowInfo.Tag != null && this.RowInfo.Tag.ToString()== "button")
            {
                Button.Visibility = Telerik.WinControls.ElementVisibility.Visible;
                Button.Arrange(new RectangleF(0, 0, result.Width, result.Height));
            }
            else
            {
                Button.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            }
            return result;
       }
    }

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Simon
    Simon avatar
    32 posts
    Member since:
    Aug 2011

    Posted 21 Dec 2016 in reply to Dimitar Link to this post

    Thank's very much Dimitar. I have one small problem with the changing of the text on the button. I currently have a single button row in the grid, but it's text keeps changing from "Add Work History" to "Add" when I scroll in the grid. The code that controls the text is:

     Private Sub RadGridView1_RowFormatting(sender As Object, e As RowFormattingEventArgs) Handles dgv.RowFormatting
            Dim row As MyDataRow = TryCast(e.RowElement, MyDataRow)
            If row IsNot Nothing AndAlso row.Button.Visibility = ElementVisibility.Visible AndAlso e.RowElement.RowInfo.Tag = "button" Then
                Select Case Headings.Where(Function(x) x.HeadingID = e.RowElement.RowInfo.Cells("HeadingID").Value).First.ObjectType
                    Case objectType.WorkHistory
                        row.Button.Text = "Add Work History"
                    Case Else
                        row.Button.Text = "Test"
                End Select
            Else
                row.Button.Text = "Add"
            End If
        End Sub

     

    The button text should always say "Add Work History" but it seems that sometimes when I scroll it doesn't detect the tag on the row and so sets the text to "Add"

     

  4. Dimitar
    Admin
    Dimitar avatar
    2951 posts

    Posted 22 Dec 2016 Link to this post

    Hello Simon,

    This is caused because of the virtualization (the row elements are reused) and it appears that the cells are updated after the row so the value is invalid. It would be better to update the row in the UpdateInfo method:
    ' the MyDataRow class
    Public Overrides Sub UpdateInfo()
        MyBase.UpdateInfo()
        Me.button.Text = Me.RowInfo.Cells(0).Value.ToString()
    End Sub

    Please do not hesitate to contact us with any additional questions or concerns. 
     
    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top