Two checkboxes in gridview

3 posts, 1 answers
  1. Zbigniew Kozłowski
    Zbigniew Kozłowski avatar
    60 posts
    Member since:
    Apr 2010

    Posted 20 Sep 2010 Link to this post

    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. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Sep 2010 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Zbigniew Kozłowski
    Zbigniew Kozłowski avatar
    60 posts
    Member since:
    Apr 2010

    Posted 20 Sep 2010 Link to this post

    Nice and clean sample cheers ;)
    Mine problem was that i didnt know how to get parent of checkbox ;)
Back to Top