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

Missing RowHeaderColumn and ContextMenu throw an Exception on CustomNewRowInfo

4 Answers 166 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Agung
Top achievements
Rank 1
Agung asked on 31 Jan 2020, 01:15 PM

Hi...

i got some problem on my custom new row element according to this link, but on my code i made with button element

internal class CustomGridNewRowElement : GridRowElement
{
    private GridCellElement cellElement;
    private RadButtonElement radButtonElement;
    public RadButtonElement AddButtton => this.radButtonElement;
 
    public CustomGridNewRowElement() { }
 
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        this.cellElement = new GridCellElement(null, this) { StretchHorizontally = true, StretchVertically = true };
        this.radButtonElement = new RadButtonElement("Add New");
        this.Children.Add(cellElement);
        this.cellElement.Children.Add(this.radButtonElement);
        this.cellElement.ClipDrawing = true;
    }
 
    public override bool IsCompatible(GridViewRowInfo data, object context)
    {
        return data is CustomGridViewRowInfo;
    }
}
 
internal class CustomGridViewRowInfo : GridViewNewRowInfo
{
    public CustomGridViewRowInfo(GridViewInfo viewInfo) : base(viewInfo)
    {
    }
 
    public override Type RowElementType => typeof(CustomGridNewRowElement);
}

the problem is im missing an rowheadercellelement as shown on attachment, i need the Add Button only fit on GridViewDataColumns like a standard GridViewNewRowInfo,

and shown on 2nd attachment it throw an exception when mouse right button press / contextmenu on Add Button Element, i couldnt find whats wrong.

any help please.

 

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Feb 2020, 09:28 AM

Hello, Agung,   

Following the provided information, I have created a sample project. Indeed, when you right-click the RadButtonElement in the new row, an error occurs. This is because there is no ColumnInfo applied to the GridCellElement that hosts the button. 

The possible solution that I can suggest is to create a custom GridCellElement and override its ShowContextMenu method. Comment the default logic and if you want to show a menu, feel free to create one: 

        internal class CustomGridNewRowElement : GridRowElement
        {
            private GridCellElement cellElement;
            private RadButtonElement radButtonElement; 

            public RadButtonElement MyProperty
            {
                get
                {
                    return this.radButtonElement;
                }
            }
 
            public CustomGridNewRowElement()
            {
            }
 
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
                this.cellElement = new CustomGridCellElement(null, this) { StretchHorizontally = true, StretchVertically = true };
                this.radButtonElement = new RadButtonElement("Add New"); 
                this.Children.Add(cellElement);
                this.cellElement.Children.Add(this.radButtonElement);
                this.cellElement.ClipDrawing = true;
            }
 
            public override bool IsCompatible(GridViewRowInfo data, object context)
            {
                return data is CustomGridViewRowInfo;
            }
        }

        public class CustomGridCellElement : GridCellElement
        { 
            public CustomGridCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
            {
            }
            protected override void ShowContextMenu()
            {
                RadContextMenu menu = new RadContextMenu();
                menu.Items.Add(new RadMenuItem("Item1"));
                menu.Show(new Point(MousePosition.X,MousePosition.Y) ); 
            }
        }

 


I hope this information helps. If you need any further assistance please don't hesitate to contact me.  

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Agung
Top achievements
Rank 1
answered on 03 Feb 2020, 02:16 PM

Hi Dess, your code works perfect. Thank you

One more thing, if it possible how RadButtonElement can fit in GridViewDataColumns only? i need the buttonelement fit on the blue rectangle, please check the attachment.

how to manipulate the GridRowHeaderCellElement.

im trying add GridRowHeaderCellElement with this code:

protected override void CreateChildElements()
{
    try
    {
        base.CreateChildElements();
        this.cellElement = new CustomGridCellElement(null, this) { StretchHorizontally = true, StretchVertically = true };
        this.AddButton = new RadButtonElement("Add New");
        this.Children.Add(new GridRowHeaderCellElement(null, this));
        this.Children.Add(cellElement);
        this.cellElement.Children.Add(this.AddButton);
        this.cellElement.ClipDrawing = true;
    }
    catch (Exception e)
    {
        throw e;
    }
}

but it throw Object reference not set to an instance of an object.

at Telerik.WinControls.UI.GridVirtualizedCellElement.BindRowProperties()

at Telerik.WinControls.UI.GridRowHeaderCellElement.BindRowProperties()
   at Telerik.WinControls.UI.GridVirtualizedCellElement.Initialize(GridViewColumn column, GridRowElement row)
   at Telerik.WinControls.UI.GridRowHeaderCellElement..ctor(GridViewColumn column, GridRowElement row)
   at CustomNewRow.CustomGridNewRowElement.CreateChildElements()

 the error i thing is cause the GridRowHeaderCellElement columninfo is null

any help?

 

 

 

0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Feb 2020, 09:26 AM

Hello, Agung,   

You can apply a left offset in the RadButtonElement.Margin property:

            protected override void CreateChildElements()
            {
                base.CreateChildElements();
                this.cellElement = new CustomGridCellElement(null, this) { StretchHorizontally = true, StretchVertically = true };
                this.radButtonElement = new RadButtonElement("Add New"); 
                this.Children.Add(cellElement);
                this.radButtonElement.Margin = new Padding(20, 0, 0, 0);
                this.cellElement.Children.Add(this.radButtonElement);
                this.cellElement.ClipDrawing = true;
            }

 

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Agung
Top achievements
Rank 1
answered on 04 Feb 2020, 12:33 PM
Hi Dess, that's not what I want. for sure the button element is move with padding, but I need to insert the RowHeaderCellElement like other rows or with the default GridViewNewRowInfo, at the moment i just set the ShowRowHeaderColumn Visible, Thank you
Tags
GridView
Asked by
Agung
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Agung
Top achievements
Rank 1
Share this question
or