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

[Solved] GridView select column

15 Answers 409 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ron klod
Top achievements
Rank 1
ron klod asked on 21 Feb 2010, 08:17 PM
Hi,

I am trying to define a select column on a grid view as I saw in your example
1. how this column can be bind ?
2. how is the header is set to a checkbox like in your example, I get some text instead of a checbox, according to your example it looks like you don't need to do anything in order to get this behaviour

Ron

15 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 22 Feb 2010, 06:40 AM
Hi ron klod,

Currently this column does not support binding. Could you please give us more information about the functionality that you would like to implement? I guess that there is a property on the data items (for exmaple, IsSelected) that you would like to bind the selected column to. If that is the case you could use a workaround - subscribe to the SelectionChanged event and update the respective data item properties when selection changes. For example:

private void cmbViews_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (MyDataType selectedItem in e.AddedItems)
    {
        selectedItem.IsSelected = true;
    }
  
    foreach (MyDataType deselectedItem in e.RemovedItems)
    {
        selectedItem.IsSelected = false;
    }
}


Regards,
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
ron klod
Top achievements
Rank 1
answered on 23 Feb 2010, 09:52 PM
thanks for the reply,
this is somthing that I have tried, and decided to this instead:
instead of using the built-in selectColumn, I have create a template column which holds a checkbox and that checkbox is bound to some property.
my question now is:
when the user checks the checkbox I want the rows to be selected, the problem is that I could not find any way to get the current row object when checking the checkbox, I have tried using the Checked and Click events on the checbox, but this does not help since it returns a reference to the cehckbox
how can I get a refernece to the row that holds the checkbox?

Ron
0
Vlad
Telerik team
answered on 24 Feb 2010, 06:57 AM
Hi Ron,

You can use ParentOfType<GridViewRow>() to achieve this. For example:

var checkBox = (CheckBox)sender;
var row = checkBox.ParentOfType<GridViewRow>();

This method is an extension method part of Telerik.Windows.Controls namespace.

Regards,
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
ron klod
Top achievements
Rank 1
answered on 24 Feb 2010, 07:42 AM
ok, I will try this
 another question:

I have a button that is used to check\uncheck checkbox on the gird
since I have a checkbox in each row of the grid, I running on all of them and check them simply ny lopping on Gird.Items
the problem I have seen is that if the grid contains a scroll them all the item which are down (I need to scroll) are not cehcked

How can I solve this?


Ron
0
Vlad
Telerik team
answered on 24 Feb 2010, 07:46 AM
Hello Ron,

If this CheckBox is bound to some property of your business objects why not change the property of the object directly?

Kind regards,
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
ron klod
Top achievements
Rank 1
answered on 24 Feb 2010, 07:57 AM
it is bound to a property, the entity is checked and it is ok, but the disaply of the grid is not OK,, when scrolling down the items are not checked
0
Vlad
Telerik team
answered on 24 Feb 2010, 08:39 AM
Hello Ron,

I've just tried this however everything worked fine on my end. Please check the attached project for reference.

All the best,
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
ron klod
Top achievements
Rank 1
answered on 24 Feb 2010, 10:22 AM
Hi
I have tried to use your example, but still O ahve prolbems.

I have made some changes in your example to be a like my project
please try to see what I did wrong there


since I can not upload my files to here
here is the MainPage,xaml.cs

using

 

System.Windows.Controls;

 

using

 

System.Collections;

 

using

 

System.Linq;

 

using

 

System.Collections.Generic;

 

using

 

System.ComponentModel;

 

using

 

Telerik.Windows.Controls;

 

using

 

Telerik.Windows.Controls.GridView;

 

namespace

 

SilverlightApplication1

 

{

 

public partial class MainPage : UserControl

 

{

 

public MainPage()

 

{

InitializeComponent();

 

//this.dg1.ItemsSource = from i in Enumerable.Range(0, 1000)

 

 

// select new MyObject() { ID = i };

 

 

MyObject obj = new MyObject();

 

 

this.DataContext = obj;

 

 

this.dg1.ItemsSource = obj.GridItem;

 

 

}

 

private void CheckBox_Click(object sender, System.Windows.RoutedEventArgs e)

 

{

 

//StringbUI

 

 

var grid = ((CheckBox)sender).ParentOfType<GridViewDataControl>();

 

 

 

//List<

 

 

foreach(MyObjec2 o in dg1.Items)

 

{

o.IsChecked = ((

CheckBox)sender).IsChecked.Value;

 

}

 

 

}

 

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)

 

{

 

//this.dg1.ItemsSource = from i in Enumerable.Range(0, 1000)

 

 

// select new MyObject() { ID = i };

 

 

}

}

 

public class MyObject : INotifyPropertyChanged

 

{

 

public MyObject()

 

{

Load();

}

 

 

//public int ID { get; set; }

 

 

private List<MyObjec2> _gridItems;

 

 

//bool _MyProperty;

 

 

//public bool MyProperty { get { return _MyProperty; } set { _MyProperty = value; OnPropertyChanged("MyProperty"); } }

 

#region

 

INotifyPropertyChanged Members

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

private void OnPropertyChanged(string propertyName)

 

{

 

if (this.PropertyChanged != null)

 

{

 

this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

 

}

}

 

private void Load()

 

{

_gridItems =

new List<MyObjec2>();

 

 

for (int i = 0; i < 1000; i++)

 

{

_gridItems.Add(

new MyObjec2() { Name = "Ron" + i.ToString(), Phone = "12345", IsChecked = false });

 

}

GridItem = GridItem;

}

 

public List<MyObjec2> GridItem

 

{

 

get { return this._gridItems; }

 

 

set

 

{

 

this._gridItems = value;

 

OnPropertyChanged(

"GridItem");

 

}

}

 

 

#endregion

}

 

public class MyObjec2

 

{

 

public string Name

 

{

 

get;

 

 

set;

 

}

 

public string Phone

 

{

 

get;

 

 

set;

 

}

 

public bool IsChecked

 

{

 

get;

 

 

set;

 

}

 

 

 

}

}

here is MainPage.xaml

 

<

 

UserControl x:Class="SilverlightApplication1.MainPage"

 

 

 

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

 

 

 

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

 

 

mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"

 

 

 

 

 

xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView">

 

 

 

 

 

 

<Grid x:Name="LayoutRoot">

 

 

 

 

 

 

 

<Grid.RowDefinitions>

 

 

 

 

 

 

<RowDefinition Height="40" ></RowDefinition>

 

 

 

 

 

 

<RowDefinition Height="200"></RowDefinition>

 

 

 

 

 

 

 

</Grid.RowDefinitions>

 

 

 

 

 

 

 

<Button Grid.Row="0" Content="clieck me" Click="Button_Click"></Button>

 

 

 

 

 

 

 

<telerikGrid:RadGridView x:Name="dg1" Grid.Row="1" AutoGenerateColumns="False">

 

 

 

 

 

 

<telerikGrid:RadGridView.Columns>

 

 

 

 

 

 

<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding IsChecked}">

 

 

 

 

 

 

<telerikGrid:GridViewDataColumn.Header>

 

 

 

 

 

 

<CheckBox Click="CheckBox_Click" />

 

 

 

 

 

 

</telerikGrid:GridViewDataColumn.Header>

 

 

 

 

 

 

<telerikGrid:GridViewDataColumn.CellTemplate>

 

 

 

 

 

 

<DataTemplate>

 

 

 

 

 

 

<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" />

 

 

 

 

 

 

</DataTemplate>

 

 

 

 

 

 

</telerikGrid:GridViewDataColumn.CellTemplate>

 

 

 

 

 

 

</telerikGrid:GridViewDataColumn>

 

 

 

 

 

 

<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Name}" />

 

 

 

 

 

 

</telerikGrid:RadGridView.Columns>

 

 

 

 

 

 

</telerikGrid:RadGridView>

 

 

 

 

 

 

</Grid>

 

</

 

UserControl>

 

0
Vlad
Telerik team
answered on 24 Feb 2010, 11:00 AM
Hello ,

If you want your TwoWay Binding to work normally you need to raise PropertyChanged.

Kind regards,
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
ron klod
Top achievements
Rank 1
answered on 24 Feb 2010, 12:03 PM
where?
0
ron klod
Top achievements
Rank 1
answered on 24 Feb 2010, 04:15 PM
can you please show me where? where do I do that in the example I sent
0
Vlad
Telerik team
answered on 25 Feb 2010, 07:31 AM
Hi,

You should raise PropertyChanged in property setter.

Regards,
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
ron klod
Top achievements
Rank 1
answered on 25 Feb 2010, 07:58 AM
but this is waht I do
and it still does not work
0
Accepted
Vlad
Telerik team
answered on 25 Feb 2010, 08:09 AM
Hello,

Nothing will raise PropertyChange in your IsChecked property - you have simple auto-property:

public bool IsChecked
{
get;
set;
}

All the best,
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
ron klod
Top achievements
Rank 1
answered on 25 Feb 2010, 11:56 AM
thanks,

I ddi not notice that
Tags
GridView
Asked by
ron klod
Top achievements
Rank 1
Answers by
Milan
Telerik team
ron klod
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or