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

Determine if checkbox is selected

4 Answers 26 Views
GridView
This is a migrated thread and some comments may be shown as answers.
GM72
Top achievements
Rank 1
GM72 asked on 12 Sep 2012, 11:37 AM
In the selection_changed event handler I want to be able to determine if the checkbox in the selected row has been selected.

I don't seem to be able to do this? Please advise

e.g.

 private void RadGridView1_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            var dataItem = e.AddedItems[0];
            bool checkBoxValue;
            if (dataItem != null)
            {
                var selectedUIRow = RadGridView1.ItemContainerGenerator.ContainerFromItem(dataItem)
                    as Telerik.Windows.Controls.GridView.GridViewRow;
                if (selectedUIRow != null)
                {
                    var firstCell = selectedUIRow.Cells[3] as Telerik.Windows.Controls.GridView.GridViewCell;
                    if (firstCell != null)
                    {
                        // Get the value from the UI without knowing the type.
                        //checkBoxValue = (bool)firstCell.Value;
                    }
                }
            }
        }

firstcell.value returns the whole data entity... which is not what I want

4 Answers, 1 is accepted

Sort by
0
GM72
Top achievements
Rank 1
answered on 12 Sep 2012, 11:52 AM
The above code works for any of the cells that are bound and I can access their natural values ie cell[1].value.

Don't know why cell[3] (the checkbox cell) returns the whole data entity?
0
Yoan
Telerik team
answered on 17 Sep 2012, 10:48 AM
Hi Graeme,

 In order to determine if the checkbox is checked you can define properties in your business model and then define GridView's columns like : 

<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Married}"/>
</telerik:RadGridView.Columns>

In the example, "Married" is of type bool, so the GridView will generate checkBox column by defaut. Then in selection_Changed event you can check the value of "Married" property is true or false. I would suggest you to work with the data objects instead of the visual elements. 

In case that does not help you, please tell me what your exact scenario is.

Greetings,
Yoan
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
GM72
Top achievements
Rank 1
answered on 17 Sep 2012, 06:04 PM
Hi Yoan,

I understand what you are saying and this would work however I don't necessarily agree that I should be altering my domain model for this purpose.

My purpose would be:

User can select checkbox on a selection of rows out of the total rows
User presses a commit button 
For each of the selected rows a certain action will take place

In my opinion this is view related logic.

What I need is someway of selecting or marking multiple rows, then, when a button is pressed, identify which rows have been selected so as I can do something to them?
0
Vlad
Telerik team
answered on 18 Sep 2012, 05:45 AM
Hello,

 You can use GridViewSelectColumn to select multiple items and you can use SelectedItems collection to check what is actually selected. 

Greetings,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Tags
GridView
Asked by
GM72
Top achievements
Rank 1
Answers by
GM72
Top achievements
Rank 1
Yoan
Telerik team
Vlad
Telerik team
Share this question
or