Loop through rows to get values

Hi again! Long time no type!

I have a query, my grid has a template field with a checkbox, I want to be able to get the value of each row checkbox when I click a button and perform a function based on the value of the check box.

How do I loop through the rows in the grid in order to get this value?

<button type="button" @onclick="SaveMany" class="button bg-theme-9 text-white defaultsize mr-1"><i class="fas fa-save mr-2"></i>Save</button>
 
 <GridColumn HeaderText="" Width="20">
                                        <Template>
                                              <input class="show-code input input--switch border" type="checkbox">
                                        </Template>
  </GridColumn>

   private async Task SaveMany()
    {
        var Rows = await BooksGrid.GetRows();
        foreach(var row in Rows)
        {
         How can I access the checkbox value in each row? 
        }
    }

3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team July 28, 2020 09:39 AM UTC

Hi John, 

Greetings from Syncfusion support. 

Query : How do I loop through the rows in the grid in order to get this value? 
We suggest you to use the CurrentViewData property of Grid. With this you can get the row data of the rows available in Grid’s current view page. Please refer and use as like the code below, 

 
private async Task SaveMany() 
{ 
    var RowsData = BooksGrid.CurrentViewData; 
    foreach (var row in RowsData) 
    { 
        //How can I access the checkbox value in each row? 
    } 
} 


Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



JO John July 28, 2020 10:24 AM UTC

Thank you foe the reply.
The field is not a date field but a template field, it seems I can't access that from the CurrentViewData?





RS Renjith Singh Rajendran Syncfusion Team July 29, 2020 11:34 AM UTC

Hi John, 

Thanks for your update. 

We suggest you to use our Syncfusion SfCheckBox instead of input element. And assign a unique value for the @ref based on a value from a unique value column(OrderID). Now with this @ref value you can fetch the current state of the Checkbox in each row by using the Checked property of SfCheckbox in SelectMany method by iterating the values fetched from CurrentViewData. We have prepared a sample based on this scenario. Please download the sample from the link below, 
 
Please refer and use as like the codes below, 

 
        <GridColumn HeaderText="" Width="20"> 
            <Template> 
                @{ 
                    var a = context as Order; 
 
                    //Assign unique @ref value for the checkboxes in each row based on OrderID column value 
                    <SfCheckBox @ref="@InputCheck[a.OrderID]" TChecked="bool"></SfCheckBox> 
                } 
                </Template> 
        </GridColumn>  
  
@code{ 
 
    SfGrid<Order> BooksGrid { getset; } 
    private Dictionary<int?, SfCheckBox<bool>> InputCheck = new Dictionary<int?, SfCheckBox<bool>>(); 
 
    private async Task SaveMany() 
    { 
        var RowsData = BooksGrid.CurrentViewData; 
        foreach (Order row in RowsData) 
        { 
            //Here you can get the state of the checkboxes in each row. Based on the ref value of each checkboxes in each rows 
            var b = InputCheck[row.OrderID].Checked; 
        } 
    } 
    ... 
} 



Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 


Marked as answer
Loader.
Up arrow icon