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

how to find the control in GridView's DataColumn CellTemplate

5 Answers 386 Views
GridView
This is a migrated thread and some comments may be shown as answers.
bluberry
Top achievements
Rank 1
bluberry asked on 06 Oct 2010, 10:01 AM
Hello,

I want get the the Label in gridview's datacolumn celltemplate.
In xaml file , the design is as follows.

 <telerik:RadGridView x:Name="ecbPropertiesGV" AutoGenerateColumns="False"
                                                 ActionOnLostFocus="CancelEdit" CanUserReorderColumns="False"
                                                 RowIndicatorVisibility="Collapsed" IsFilteringAllowed="False"
                                                 CanUserDeleteRows="False" CanUserInsertRows="False" CanUserSelect="False"
                                                 CanUserSortColumns="False" IsReadOnly="True" ShowGroupPanel="False" CanUserResizeColumns="False">
                                <telerik:RadGridView.Columns>
                                    <telerik:GridViewDataColumn Header="Number."  x:Name="numberColumn" HeaderTextAlignment="Center">
                                        <telerik:GridViewDataColumn.CellTemplate>
                                            <DataTemplate>
                                                <Label x:Name="lblName" Width="170" VerticalAlignment="Center" />
                                            </DataTemplate>
                                        </telerik:GridViewDataColumn.CellTemplate>
                                    </telerik:GridViewDataColumn>
</telerik:RadGridView>

How to find the lblName(Label)  in GridViewDataColumn.
If anyone who knows about this , please reply me.

Best Regards

5 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 06 Oct 2010, 10:08 AM
Hi bluberry,

You may take advantage of the extension method ChildrenOfType<> and find the Label inside the cell. More information about this method and its usage can be find in this blog post.

Regards,
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
bluberry
Top achievements
Rank 1
answered on 06 Oct 2010, 10:47 AM
Hello,
I try to use with by the following code,but still can't , i got just null.
Please help me how can i do it.

<telerik:RadGridView x:Name="myGridView" AutoGenerateColumns="False"
                                                 ActionOnLostFocus="CancelEdit" CanUserReorderColumns="False"
                                                 RowIndicatorVisibility="Collapsed" IsFilteringAllowed="False"
                                                 CanUserDeleteRows="False" CanUserInsertRows="False" CanUserSelect="False"
                                                 CanUserSortColumns="False" IsReadOnly="True" ShowGroupPanel="False" CanUserResizeColumns="False">
                                <telerik:RadGridView.Columns>
                                    <telerik:GridViewDataColumn Header="Number."  x:Name="numberColumn" HeaderTextAlignment="Center">
                                        <telerik:GridViewDataColumn.CellTemplate>
                                            <DataTemplate>
                                                <Label x:Name="lblName" Width="170" VerticalAlignment="Center" />
                                            </DataTemplate>
                                        </telerik:GridViewDataColumn.CellTemplate>
                                    </telerik:GridViewDataColumn>
</telerik:RadGridView>
I put this code after InitializeComponent method.
var row = myGridView.ParentOfType<GridViewRow>();
var labelValue = row.ChildrenOfType<Label>().Where(tb => tb.Name == "lblName").FirstOrDefault();

Best Regards.
0
Maya
Telerik team
answered on 06 Oct 2010, 11:24 AM
Hello bluberry,

The reason for you to get "null" value is that not all the visual elements, including the rows, are created. What you may do is to handle the RowLoaded event, but still you will be able to get the elements only inside the rows that have been loaded. So, in order to provide you with an appropriate for your exact scenario solution, I would need a bit more details about your requirements.
 

Regards,
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
bluberry
Top achievements
Rank 1
answered on 06 Oct 2010, 01:01 PM
Hello,
My requirement is simple.I just want to get the Label control in GridViewDataColumn's CellTemplate.


 <telerik:RadGridView x:Name="PropertiesGV" AutoGenerateColumns="False"
                                                 ActionOnLostFocus="CancelEdit" CanUserReorderColumns="False"
                                                 RowIndicatorVisibility="Collapsed" IsFilteringAllowed="False"
                                                 CanUserDeleteRows="False" CanUserInsertRows="False" CanUserSelect="False"
                                                 CanUserSortColumns="False" IsReadOnly="True" ShowGroupPanel="False" CanUserResizeColumns="False">
                                <telerik:RadGridView.Columns>
                                    <telerik:GridViewDataColumn Header="Number."  x:Name="numberColumn" HeaderTextAlignment="Center">
                                        <telerik:GridViewDataColumn.CellTemplate>
                                            <DataTemplate>
                                                <Label x:Name="lblName" Width="170" VerticalAlignment="Center" />
                                            </DataTemplate>
                                        </telerik:GridViewDataColumn.CellTemplate>
                                    </telerik:GridViewDataColumn>
</telerik:RadGridView>

I just want to get the Label control ,it's name is lblName.If you can, please help me.

Best Regards,
0
Mark Richardson
Top achievements
Rank 1
answered on 19 Jul 2011, 08:06 PM
Hello,

I am not sure if this is the best way, but here is what I did in this situation.

                                                <telerikGridView:GridViewDataColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <CheckBox Click="CheckBox_FilterBy_Click" Loaded="CheckBox_FilterBy_Loaded" IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Description}" />
                                                    </DataTemplate>
                                                </telerikGridView:GridViewDataColumn.CellTemplate>


In my code behind I did this.

        private List<CheckBox> _filterByCheckBoxList = null;

        private void CheckBox_FilterBy_Loaded(object sender, RoutedEventArgs e)
        {
            CheckBox c = sender as CheckBox;

            if(_filterByCheckBoxList == null)
            {
                _filterByCheckBoxList = new List<CheckBox>();
            }

            _filterByCheckBoxList.Add(c);
        }

So now I have a reference to each item loaded.

Thanks,
Mark Richardson
one stop solutions for small business
Tags
GridView
Asked by
bluberry
Top achievements
Rank 1
Answers by
Maya
Telerik team
bluberry
Top achievements
Rank 1
Mark Richardson
Top achievements
Rank 1
Share this question
or