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

get all row with checkboxcolumn checked

1 Answer 845 Views
GridView
This is a migrated thread and some comments may be shown as answers.
pcprogrammer9
Top achievements
Rank 1
pcprogrammer9 asked on 12 Dec 2019, 07:23 AM

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);
    }
}

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

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 13 Dec 2019, 03:01 PM

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.
Tags
GridView
Asked by
pcprogrammer9
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or