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

Check/Uncheck checkbox inside GridView

10 Answers 878 Views
GridView
This is a migrated thread and some comments may be shown as answers.
RoxanaC
Top achievements
Rank 1
RoxanaC asked on 06 Apr 2010, 02:08 PM
Hello!

There is a detail that I need to change somehow.
I use a simple GridView bounded to a ObservableCollection of objects. These objects have a boolean property that it's represented in the table as a checkbox. 
My case is similar with the GridView/CellEditTemplate example
The only operation I do on this GridViev is to check or uncheck these check-boxes in order to use the checked items further in my program.
Now, in order to do that I have to make 3 mouse clicks: one for selecting the row, one for selecting the cell and one for check/uncheck. Since all my columns are read only excepting that one I've mentioned before, I would like to check/uncheck an item from the first click, or at least from the second one.
I've tried to add behavior for double click for example

private void radGridView_MouseDoubleClick( object sender, MouseButtonEventArgs e ) { 
            RadGridView radGrid = (RadGridView)sender; 
            DataInfo actualRow = (DataInfo)radGrid.SelectedItem; 
            actualRow.IsSelected = actualRow.IsSelected == false; 
        } 

but this will change my DataInfo object without actually checking it's combobox in the user interface.
Of course, you would say to call radGrid.Rebind(); method, but in this case, for a 100 objects collection the entire focus of this table will move up to the first one (considering that I make the checking on a row in the end of the table) and I'm not happy with that.

Is there other work around for this?

Hopping that I was coherent enough!
Best wishes,
Roxana C



10 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 06 Apr 2010, 02:20 PM
Hello RoxanaC,

Why not set this CheckBox in CellTemplate instead?

Sincerely yours,
Vlad
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
RoxanaC
Top achievements
Rank 1
answered on 06 Apr 2010, 02:23 PM
Hmmm
how could I do that?
0
RoxanaC
Top achievements
Rank 1
answered on 06 Apr 2010, 02:30 PM
Got it!

<Controls:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsSelected}" />
                            </DataTemplate>
</Controls:GridViewDataColumn.CellTemplate>

Thank you!
RoxanaC
0
RoxanaC
Top achievements
Rank 1
answered on 04 May 2010, 08:12 AM
Hello Vlad!
Back to you with an issue related with this check-box type column of a grid view.
I am binding my columns to a custom objects collection

 <DataTemplate x:Key="ContentTemplate"
            <Controls:RadGridView x:Name="radGridViewDataBinding" 
                                  ScrollMode="RealTime" 
                                  ShowGroupPanel="False" 
                                  CanUserReorderColumns="False" 
                                  CanUserDeleteRows="False" 
                                  CanUserInsertRows="False" 
                                  Grid.Row="1" 
                                  ItemsSource="{Binding Metadata}" 
                                  AutoGenerateColumns="False"
                <Controls:RadGridView.Columns> 
                    <Controls:GridViewDataColumn DataMemberBinding="{Binding Name}" 
                                                 Header="Symbol" 
                                                 IsReadOnly="True"/> 
                    <Controls:GridViewDataColumn DataMemberBinding="{Binding ClearName}" 
                                                 Header="Name" 
                                                 IsReadOnly="True"/> 
                    <Controls:GridViewDataColumn DataMemberBinding="{Binding Description}" 
                                                 Header="Description" 
                                                 IsReadOnly="True"/> 
                    <Controls:GridViewDataColumn DataMemberBinding="{Binding Source}" 
                                                 Header="Source" 
                                                 IsReadOnly="True"/> 
                    <Controls:GridViewDataColumn DataMemberBinding="{Binding Unit}" 
                                                 Header="Unit" 
                                                 IsReadOnly="True"/> 
                    <Controls:GridViewDataColumn DataMemberBinding="{Binding Category}" 
                                                 Header="Category" 
                                                 IsReadOnly="True"/> 
                    <Controls:GridViewDataColumn Header="Select " IsReadOnly="True"
                        <Controls:GridViewDataColumn.CellTemplate> 
                            <DataTemplate> 
                                <CheckBox IsChecked="{Binding Used, Mode=TwoWay}" HorizontalAlignment="Center"/> 
                            </DataTemplate> 
                        </Controls:GridViewDataColumn.CellTemplate> 
                    </Controls:GridViewDataColumn> 
                </Controls:RadGridView.Columns> 
 
            </Controls:RadGridView> 
        </DataTemplate> 

My problem is that all my columns can be sorted but not this one containing check-boxes.
I need to be able to push the header of the column and have the rows arranged by "IsChecked" property of each check-box.
Is this possible?

Thank you!
Roxana C




0
Accepted
Vlad
Telerik team
answered on 04 May 2010, 08:18 AM
Hello RoxanaC,

If you set DataMemberBinding for this column you will able to sort it. In your case this will be Binding to Used property.

Best wishes,
Vlad
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
RoxanaC
Top achievements
Rank 1
answered on 04 May 2010, 08:27 AM
...So this was the missing puzzle piece :)
Thank you Vlad!

0
Chad
Top achievements
Rank 1
answered on 12 Jul 2010, 11:00 PM
I'm having a binding problem, this is a close example. Can you fill in the corrected code that was used for the CellTemplate using DataMemberBinding.

My problem is that I'm getting in the Immediate Window BindingExpression error that the bit field is not found in the DataRow. I'm binding to a Datatable.
0
Milan
Telerik team
answered on 13 Jul 2010, 04:38 AM
Hi Chad,

Could you please paste your binding definition.


Greetings,
Milan
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
Chad
Top achievements
Rank 1
answered on 13 Jul 2010, 06:34 PM
Okay I did solve the issue. Here's how to fix it, basically remove the CellTemplate node, apparently GridViewDataColumn shows a checkbox if it detects that the datatype is a bit. Which is fine. Still would like to know why it won't work though. 

Step 1)  From the Demo that come with the install find GridView\CellEditTemplate\Example.xaml
<telerik:GridViewDataColumn Header="Discontinued"
   DataMemberBinding="{Binding Discontinued, Mode=TwoWay}"
   TextAlignment="Center">
</telerik:GridViewDataColumn>

Step 2) Change grid Binding to GetDTRandomProducts
Step 3) Add new property in GridView\GridViewExample.cs
public DataTable GetDTRandomProducts
{
    get
    {
        SqlConnection conn = new SqlConnection(Settings.Default.NorthwindConnectionString);
        SqlCommand cmd = new SqlCommand("select ProductID as ID, ProductName as Name, UnitPrice, GETDATE() as Date, Discontinued from Products", conn);
        try
        {
            conn.Open();
            cmd.CommandType = System.Data.CommandType.Text;
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Load(cmd.ExecuteReader());
            return dt;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
}

Step 4) Run and it all works.

If you want to see the binding error, then put back the code CellTemplate code that uses IsChecked that binds to Discontinued column.
<telerik:GridViewDataColumn.CellTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Discontinued, Mode=TwoWay}" IsEnabled="False" />
    </DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>

Removing CellTemplate is fine and so it works, but it would be nice to know why it failed to work and generated the binding expression error.

Thanks ... Chad
0
Milan
Telerik team
answered on 14 Jul 2010, 04:06 PM
Hello Chad,

The CellTemplate does not work because there is no property Discontinued on the data items that the grid is bound to - in your case the data items are of type DataRow. In this case the correct binding expressions is "Binding [Discontinued], Mode=TwoWay}". 


Sincerely yours,
Milan
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
Tags
GridView
Asked by
RoxanaC
Top achievements
Rank 1
Answers by
Vlad
Telerik team
RoxanaC
Top achievements
Rank 1
Chad
Top achievements
Rank 1
Milan
Telerik team
Share this question
or