get all row with checkboxcolumn checked

2 posts, 0 answers
  1. pcprogrammer9
    pcprogrammer9 avatar
    3 posts
    Member since:
    Sep 2019

    Posted 12 Dec 2019 Link to this post

    How I can search through all row of my grid and detect only row with checkbox column checked ?  ( Note : user change value and select checkbox at run time ... )

    i use this code and got an error : 

    ------------------------------------------------------------------------------------------------------------------------------------

    Error :

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    Telerik.WinControls.UI.GridViewCellInfo.Value.get returned null.

    ------------------------------------------------------------------------------------------------------------------------------------

    Code : 

    IList<GridViewRowInfo> gridRows = new List<GridViewRowInfo>();
    foreach (GridViewRowInfo rowInfo in radGridView.ChildRows)
    {
        bool isChecked = (bool)rowInfo.Cells["CheckboxColumn"].Value;
        if (isChecked == true)
        {
            gridRows.Add(rowInfo);
        }
    }

    ------------------------------------------------------------------------------------------------------------------------------------

  2. Nadya
    Admin
    Nadya avatar
    303 posts

    Posted 13 Dec 2019 Link to this post

    Hello,

    Iterating through the rows collection of the RadGridView in order to check the cell value in the GridViewCheckBoxColumn is the right approach to achieve this. As to the System.NullReferenceException error that you get, could you please make sure that you have a column with the same name in your grid. The checkbox column's data source must contain the respective values as well. I prepared an example for your reference:

    public RadForm1()
    {
        InitializeComponent();
        DataTable dt = new DataTable();
        dt.Columns.Add("PersonID", typeof(int));
        dt.Columns.Add("LastName", typeof(string));
        dt.Columns.Add("FirstName", typeof(string));
        dt.Columns.Add("IsChecked", typeof(bool));
        dt.Rows.Add(1, "Davolio", "Nancy", true);
        dt.Rows.Add(2, "Fuller", "Andrew", false);
        dt.Rows.Add(3, "Leverling", "Janet", true);
        dt.Rows.Add(4, "Dodsworth", "Anne", false);
        dt.Rows.Add(5, "Wolfeschlegel", "Hubert", true);
        dt.Rows.Add(6, "Woldorf", "Hubert", true);
        dt.Rows.Add(7, "Addams", "Mary", true);
    
        this.radGridView1.DataSource = dt;
        GridViewCheckBoxColumn checkBoxColumn = this.radGridView1.Columns["IsChecked"] as GridViewCheckBoxColumn;
    
        IList<GridViewRowInfo> gridRows = new List<GridViewRowInfo>();
        foreach (GridViewRowInfo rowInfo in radGridView1.ChildRows)
        {
            bool isChecked = (bool)rowInfo.Cells["IsChecked"].Value;
            if (isChecked == true)
            {
                gridRows.Add(rowInfo);
            }
        }
        Console.WriteLine(gridRows);
    }
    

    Note that GridViewCheckBoxColumn has the EditMode property that controls when the value of the editor will be submitted to the cell. By default, the current behavior is kept (OnValidate) and the value will be submitted only when the current cell changes or the grid loses focus. The new value (OnValueChange) will submit the value immediately after the editor value changes.

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Nadya
    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.
Back to Top