Setting EnableHeaderCheckBox to true causes a NullReferenceException

1 Answer 64 Views
GridView
lou
Top achievements
Rank 1
lou asked on 27 Jul 2023, 09:54 AM

Add a GridViewCheckBoxColumn to the GridView and set its EnableHeaderCheckBox property to true. An exception is caused when the column is hidden and the Readonly property of the GridView is set.

The test code is as follows: Press Btn1 and then Btn2 to trigger an exception.
public partial class RadForm1 : RadForm
{
    private bool flag1 = false;
    private bool flag2 = false;
    private GridViewCheckBoxColumn checkColumn;

    public RadForm1()
    {
        InitializeComponent();
        AddCheckColumn();
        AddButton();

        this.radGridView1.RowCount = 10;
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.MasterTemplate.AllowAddNewRow = false;
    }

    private void AddCheckColumn()
    {
        checkColumn = new GridViewCheckBoxColumn();
        checkColumn.Name = "Select";
        checkColumn.HeaderText = "All";
        checkColumn.EnableHeaderCheckBox = true;
        this.radGridView1.Columns.Insert(0, checkColumn);
        this.radGridView1.HeaderCellToggleStateChanged += RadGridView1_HeaderCellToggleStateChanged;

        this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
        this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
    }

    private void RadGridView1_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)
    {
        MessageBox.Show($"State:{e.State}");
    }

    private void AddButton()
    {
        Button btn1 = new Button();
        btn1.Text = "Btn1";
        btn1.Click += (o, e) =>
        {
            checkColumn.IsVisible = flag1;
            flag1 = !flag1;
        };
        btn1.Location = new Point(5, 5);
        this.Controls.Add(btn1);

        Button btn2 = new Button();
        btn2.Text = "Btn2";
        btn2.Click += (o, e) =>
        {
            if (flag2)
                radGridView1.Hide();
            else
            {
                radGridView1.Show();
                try
                {
                    radGridView1.ReadOnly = flag2;
                }
                catch { }
            }
            flag2 = !flag2;
        };
        btn2.Location = new Point(120, 5);
        this.Controls.Add(btn2);
    }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 31 Jul 2023, 01:36 PM

Hello, lou,

Using the provided code snippet. I have prepared a sample project to test the behavior on my side with the latest version R2 2023 SP1. However, I was unable to reproduce the issue you are facing. Please refer to the attached gif file illustrating the behavior on my end with the specified version. I have attached my sample project. Please give it a try and see how it works on your end. Am I missing something? Could you please specify the exact steps how to reproduce the problem? Thank you in advance. 

I am looking forward to your reply.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

lou
Top achievements
Rank 1
commented on 01 Aug 2023, 01:35 AM

Hi Dess,

The latest version is OK, I use the previous version R1 2023 SP1, thank you for your answer.

Tags
GridView
Asked by
lou
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or