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

WPF GridView and CheckBox column

24 Answers 990 Views
GridView
This is a migrated thread and some comments may be shown as answers.
MiroslavStantic
Top achievements
Rank 1
Iron
MiroslavStantic asked on 05 Sep 2008, 12:18 PM
Hi,

How do I define a checkbox column in WPF GridView control?

Thanks

Miroslav

24 Answers, 1 is accepted

Sort by
0
Dimitar Dobrev
Telerik team
answered on 09 Sep 2008, 06:47 AM
Hi Miroslav,

For a column that allows its cells to be edited using a check box, you may take a look at the example about "Editors" in our sample project. The "Boolean" column in the example displays a check box when one of its cells enters edit mode (double click).
However, if you want to use check boxes all the time instead of displaying text, you can find a sample in the attached archive. It demonstrates changing a Boolean property of a custom object by using check boxes in a column. You will also see, in the text blocks in the bottom, that the changes you make to a cell in this column is reflected in the underlying object.
We will be glad to answer any other questions that interest you.
 
Best wishes,
Dimitar Dobrev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Scott
Top achievements
Rank 1
answered on 31 Oct 2008, 07:51 PM
How do I set the checkbox to checked when I click on a row?

I want to be able to click anywhere on a row and check/uncheck the checkbox based on the selected property of the row.


0
Atanas
Telerik team
answered on 03 Nov 2008, 01:33 PM
Hi Scott,

I am sending you a modified copy of the sample project you already have, which in my opinion solves your issue.

There is an issue with our routed events that are handled earlier than expected. This is fixed in our new release, but to overcome it now, I need to subscribe explicitly for these events. The code looks like this:

this.radGridViewCustomers.AddHandler(RadGridView.MouseLeftButtonDownEvent, new RoutedEventHandler(radGridViewCustomers_MouseLeftButtonDown), true); 


The last Boolean parameter is very important - the 'True' value means that I am subscribed for already handled events.

After that, everything in the sample project is quite straightforward and in the event handler I am getting the underlying business object,

Record record = (e.Source as RadGridView).CurrentRecord; 
            Customer cutomer = (Customer) (record as DataRecord).Data; 
            cutomer.HasOrders = true

and I am changing the value of HasOrders property.

If you have any further questions, please let me know.




Regards,
Atanas
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Scott
Top achievements
Rank 1
answered on 21 Nov 2008, 07:52 PM
I opened up this project and ran it.  Clicking on a row does not uncheck the checkbox, so I added:
cutomer.HasOrders = !cutomer.HasOrders; 

 After a couple clicks on different rows, it throws an "Object reference not set to an instance....."  message on this line:

  Customer cutomer = (Customer) (record as DataRecord).Data; 

I see this behavior on some of my other controls...It appears that if you click too fast, you get this.  Any solutions?
0
Nedyalko Nikolov
Telerik team
answered on 25 Nov 2008, 10:42 AM
Hi Scott,

Thanks for the feedback, we have already fixed the "fast clicking" issue. Attached is a modified copy of the example you already have.

Modifications are:

 1. Changed references to point to the new Q3 assemblies.
 2. Removed subscription to the GridViewRow.MouseLeftButtonDownEvent (not needed any more).

If you have any further questions, please let me know.

Kind regards,
Nedyalko Nikolov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Scott
Top achievements
Rank 1
answered on 26 Nov 2008, 07:49 PM
Thank you, this works great.  One or two comments:

what does this mean?
2. Removed subscription to the GridViewRow.MouseLeftButtonDownEvent (not needed any more).

The MouseLeftButtonDownEvent is still there.

Another question is this.. This works great except that clicking directly on the checkbox doesn't fire the MouseLeftButtonDownEvent (which changes the business objects property, HasOrders).  Any strategy for clicking the checkbox and still changing the underlying property?    Basically I want the user to click anywhere on the row or the checkbox.

Thanks!
0
Nedyalko Nikolov
Telerik team
answered on 28 Nov 2008, 03:03 PM
Hello Scott,

About "Removed subscription to the GridViewRow.MouseLeftButtonDownEvent (not needed any more)" I mean that the previous version of the example have this subscription. You can diff MainWindow.xaml.cs but it is not so important.

Now for the issue about check CheckBox directly there is a problem with CheckBox Binding.
Xaml for the whole custom cellTemplate should look like this:

<ControlTemplate x:Key="cellTemplate" TargetType="{x:Type telerik:GridViewCell}"
                <CheckBox IsChecked="{Binding Path=HasOrders, Mode=TwoWay}"  
                          HorizontalAlignment="Center" VerticalAlignment="Center" /> 
            </ControlTemplate> 

I'm also attaching a modified example.

Sincerely yours,
Nedyalko Nikolov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Scott
Top achievements
Rank 1
answered on 24 Dec 2008, 03:13 PM
This has been working great, however I just discovered an issue.  When clicking on a column to sort, it will fire the MouseLeftButtonDown event and check or uncheck whatever record happens to be selected.  So we need a way to avoid this code when not actually clicking on a row.
0
Nedyalko Nikolov
Telerik team
answered on 05 Jan 2009, 03:45 PM
Hi Scott,

To fix the issue you can use the OriginalSource property of the RoutedEventArgs object and ensure that left mouse button is clicked on a "normal" GridViewRow.

I'm attaching the example you already have with small changes that fixes the issue.

Regards,
Nedyalko Nikolov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
nbahl
Top achievements
Rank 1
answered on 09 Feb 2009, 09:10 PM
I have a RadGridView that has two checkbox columns both binding to different fields.  Do I need to create a seperate ControlTemplate for each column?  Do you have an example of a GridView with multiple checkbox columns?

Thanks,
Nate
0
Hristo Deshev
Telerik team
answered on 11 Feb 2009, 10:57 AM
Hello nbahl,

You can reuse your templates by specifying the same resource with the StaticResource extension. Say I want to add another column besides HasOrders. I can do it like that:

<telerik:GridViewDataColumn HeaderText="HasOrders" CellStyle="{StaticResource booleanCellStyle}" /> 
<telerik:GridViewDataColumn HeaderText="HasSomethingElse" CellStyle="{StaticResource booleanCellStyle}" /> 
 

I am attaching the modified project for your reference.

Best wishes,
Hristo Deshev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
nbahl
Top achievements
Rank 1
answered on 11 Feb 2009, 02:17 PM
Hristo,
I am able to display the second checkbox but now they are both binded to the same field (HasOrders) so when you check one column it checks the other column (HasSomethingelse).  I was thinking I would need to create another ControlTemplate that is binded to a different field but when I try this Visual Studio 2008 locks up on me.  Is that what I need to do?

Thanks,
Nate
0
Stefan Dobrev
Telerik team
answered on 11 Feb 2009, 03:18 PM
Hi Nate,

This is exactly what you have to do. Just copy the control template and the style, change their keys and change the binding to point to the new property. Something like this:

<ControlTemplate x:Key="HasSomethingElseCellTemplate" TargetType="{x:Type telerik:GridViewCell}">

    <CheckBox IsChecked="{Binding Path=HasSomethingElse, Mode=TwoWay}"

            HorizontalAlignment="Center" VerticalAlignment="Center" />

</ControlTemplate>

<Style x:Key="HasSomethingElseCellStyle">

    <Setter Property="telerik:GridViewCell.Template" Value="{StaticResource HasSomethingElseCellTemplate}" />

</Style>


Note that you should also change the StaticResource in the new GridViewDataColumn's CellStyle to point to the new Style:

<telerik:RadGridView Name="radGridViewCustomers"

    AutoGenerateColumns="False" Grid.ColumnSpan="2" ItemsSource="{Binding}"

    MouseLeftButtonDown="radGridViewCustomers_MouseLeftButtonDown">

    <telerik:RadGridView.Columns>

        <telerik:GridViewDataColumn HeaderText="Name" UniqueName="Name" />

        <telerik:GridViewDataColumn HeaderText="Age" UniqueName="Age" />

        <telerik:GridViewDataColumn HeaderText="HasOrders" CellStyle="{StaticResource booleanCellStyle}" />

        <telerik:GridViewDataColumn HeaderText="HasSomethingElse" CellStyle="{StaticResource HasSomethingElseCellStyle}" />

    </telerik:RadGridView.Columns>

</telerik:RadGridView>



Hope this will help.

Greetings,
sdobrev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jonathan
Top achievements
Rank 1
answered on 02 Aug 2009, 11:10 AM
Hi,
The solutions described so far have me duplicating the ControlTemplate for each checkbox column in my application whenever the Path of the Binding is different. This is exactly what I'm trying to avoid. 
I'm looking to define one ControlTemplate so that all grid instances in my application can contain CheckBox columns without having to define a new ControlTemplate for each column (when the only thing that has changed is the path of the binding).
How should I go about defining such a solution?

Thanks,
Jonathan
0
Stefan Dobrev
Telerik team
answered on 04 Aug 2009, 11:55 AM
Hello Jonathan,

You can use global check box style to achieve this:
<Grid> 
    <Grid.Resources> 
        <Style TargetType="CheckBox"
            <Setter Property="HorizontalAlignment" Value="Center" /> 
            <Setter Property="VerticalAlignment" Value="Center" /> 
        </Style> 
    </Grid.Resources> 
    <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <telerik:RadGridView Name="playersGrid" Grid.Row="1" ColumnsWidthMode="Fill">

    </telerik:RadGridView> 
</Grid> 

Hope this helps.

All the best,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Bhavin
Top achievements
Rank 1
answered on 30 Sep 2009, 03:25 PM
I m using WPF RadGridView, now i want to make 1st record default as selected. So when i click on that tab, first record should come selected in gridview in that tab.

 Please let me know abt this.

Bhavin
0
Vlad
Telerik team
answered on 01 Oct 2009, 05:36 AM
Hi bhavin patel,

You can use SelectedItem to achieve your goal similar to our online demos.

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Bhavin
Top achievements
Rank 1
answered on 06 Nov 2009, 07:50 PM
Hello,
 i have checkbox column in radgridview. I want to unhook that checkbox's event in some other event and again hook it back. Now my problem is how to get that checkbox control from grid ??
0
Joel Palmer
Top achievements
Rank 2
answered on 10 Feb 2010, 04:07 AM
What are our chances of you just embedding a CheckBoxColumn type in an upcoming release?
0
Vlad
Telerik team
answered on 10 Feb 2010, 06:35 AM
Hi,

We already have a column with checkboxes designed for rows selection. Is that what you are looking for?

Greetings,
Vlad
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Joel Palmer
Top achievements
Rank 2
answered on 10 Feb 2010, 04:55 PM
It is if I can bind the checkbox to a nullable boolean property on my List.  Do you have an example?
0
Vlad
Telerik team
answered on 11 Feb 2010, 07:41 AM
Hello,

This is column is already bound to the row IsSelected property. Can you post more info about your scenario? Are you going to use the column for editing or selection?

Greetings,
Vlad
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Rikin
Top achievements
Rank 1
answered on 22 Feb 2010, 05:33 PM
i have one databse with 3 column,,,,fields= name password,.. now if i want to dispay these 2 field in the gridview in WPF then wht shuld i do for  tht ? please help..
and please tell me can we drag and drop all the fields from datasource into WPF .xaml form as we do in winforms.. if yes thn plz tell how ?    
0
sarag
Top achievements
Rank 1
answered on 24 May 2011, 10:14 PM
Hi,

I am having the same issue, please update if you find any solution for the multiple check box in the grid view with one control template.


thanks.



The solutions described so far have me duplicating the ControlTemplate for each checkbox column in my application whenever the Path of the Binding is different. This is exactly what I'm trying to avoid. 
I'm looking to define one ControlTemplate so that all grid instances in my application can contain CheckBox columns without having to define a new ControlTemplate for each column (when the only thing that has changed is the path of the binding).
How should I go about defining such a solution?

<Grid> 
    <Grid.Resources> 
        <Style TargetType="CheckBox"
            <Setter Property="HorizontalAlignment" Value="Center" /> 
            <Setter Property="VerticalAlignment" Value="Center" /> 
        </Style> 
    </Grid.Resources> 
    <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <telerik:RadGridView Name="playersGrid" Grid.Row="1" ColumnsWidthMode="Fill">

    </telerik:RadGridView> 
</Grid> 
Tags
GridView
Asked by
MiroslavStantic
Top achievements
Rank 1
Iron
Answers by
Dimitar Dobrev
Telerik team
Scott
Top achievements
Rank 1
Atanas
Telerik team
Scott
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
nbahl
Top achievements
Rank 1
Hristo Deshev
Telerik team
Stefan Dobrev
Telerik team
Jonathan
Top achievements
Rank 1
Bhavin
Top achievements
Rank 1
Vlad
Telerik team
Joel Palmer
Top achievements
Rank 2
Rikin
Top achievements
Rank 1
sarag
Top achievements
Rank 1
Share this question
or