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

Two checkboxes in gridview

2 Answers 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Zbigniew Kozłowski
Top achievements
Rank 1
Zbigniew Kozłowski asked on 20 Sep 2010, 11:40 AM
Hi,
i dont know how to achive mine problem, i got a GridView with two checkboxes Yes and No, when user click Yes, No should be Unchecked and when user clicks No, than Yes should be unchecked.

<telerik:RadGridView Name="listGridView" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Tak" Width="40" IsReadOnly="True">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding CheckYes, Mode=TwoWay}" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Nie" Width="40" IsReadOnly="True">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding CheckNo, Mode=TwoWay}" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Nazwa" Width="*" UniqueName="Description" IsReadOnly="True"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

Also i need to save current row to database when something is clicked (Yes or No, cos default is none) so i need to fire an event.

Last question is how can i select current row on checkbox click ?

2 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 20 Sep 2010, 12:44 PM
Hello Zbigniew Kozłowski,

You can handle the Checked events of the two CheckBox-es and apply the logic you need. For example:

private void YesCheckBox_Checked(object sender, RoutedEventArgs e)
{
    CheckBox checkBox = sender as CheckBox;
    GridViewRow parentRow = checkBox.ParentOfType<GridViewRow>();
    if (parentRow != null)
    {
        parentRow.IsSelected = true;
        Player player = this.playersGrid.SelectedItem as Player;
        if (player.CheckNo == true)
        {
            player.CheckNo = false;
            player.CheckYes = true;
        }
    }
}

The logic will be exactly the opposite for the other CheckBox. The code-snippet above also demonstrates how to set the corresponding row to be selected on clicking on the CheckBox. 
As for saving the data, you can call the CommitEdit() method of the grid.
I am sending you a sample project illustrating the proposed solution.
 


Sincerely yours,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Zbigniew Kozłowski
Top achievements
Rank 1
answered on 20 Sep 2010, 01:36 PM
Nice and clean sample cheers ;)
Mine problem was that i didnt know how to get parent of checkbox ;)
Tags
GridView
Asked by
Zbigniew Kozłowski
Top achievements
Rank 1
Answers by
Maya
Telerik team
Zbigniew Kozłowski
Top achievements
Rank 1
Share this question
or