Getting Values from GridView with ComboBox in Template

2 posts, 0 answers
  1. IT
    IT avatar
    5 posts
    Member since:
    May 2008

    Posted 09 Oct 2011 Link to this post

    I have a checkbox defined in a template.  The purpose of the grid is to list attendees of a meeting.  When I click on the checkbox, I want to get the value from the colWorkplaceID column.  I'm struggling on how to do that.  Any pointers would be greatly appreciated.  This is the definition of the gridview.  The two columns that I'm interested in are colWorkplaceID and colAttending.

    <telerik:RadGridView x:Name="RadGridViewAttendees" CanUserFreezeColumns="False"
                                        RowIndicatorVisibility="Collapsed"
                                        telerik:Theming.Theme="Windows7" AutoGenerateColumns="False"
                                        CanUserDeleteRows="False" CanUserInsertRows="False" CanUserReorderColumns="False"
                                        ClipboardCopyMode="Default" ClipboardPasteMode="None" ShowGroupPanel="False"
                                        IsReadOnly="True" EnableColumnVirtualization="False" >
                       <telerik:RadGridView.Columns>
                           <telerik:GridViewDataColumn UniqueName="colWorkplaceID" Header="WorkplaceID" DataMemberBinding="{Binding WorkplaceID}" IsVisible="False" />
                           <telerik:GridViewDataColumn UniqueName="colAttending" Header="Attending">
                               <telerik:GridViewDataColumn.CellTemplate>
                                   <DataTemplate>
                                       <CheckBox IsChecked="{Binding Attending, Mode=TwoWay}" Click="CheckBox_Click"/>
                                   </DataTemplate>
                               </telerik:GridViewDataColumn.CellTemplate>
                           </telerik:GridViewDataColumn>
                           <telerik:GridViewDataColumn UniqueName="colInsured" Header="Insured" DataMemberBinding="{Binding InsuredName}" />
                           <telerik:GridViewDataColumn UniqueName="colContact" Header="Contact" DataMemberBinding="{Binding InsuredPrimaryContactName}" />
                           <telerik:GridViewDataColumn UniqueName="colAddress" Header="Address" DataMemberBinding="{Binding WorkplaceAddress1And2}" />
                           <telerik:GridViewDataColumn UniqueName="colCityStateZip" Header="City, State, ZIP" DataMemberBinding="{Binding WorkplaceCityStateZip}" />
                           <telerik:GridViewDataColumn UniqueName="colPhone" Header="Phone" DataMemberBinding="{Binding InsuredPrimaryContactPhone}" />
                           <telerik:GridViewDataColumn UniqueName="colIndustry" Header="Industry" DataMemberBinding="{Binding InsuredIndustryCode}" />
                       </telerik:RadGridView.Columns>
                   </telerik:RadGridView>

    The click event on the checkbox works fine.  I simply cannot figure out how to get the workplaceID from the cell in the same row as the checbox.  The ParentRow gives me lots of information about the cells, but I'm struggling on how to get the underlying data value.

    private void CheckBox_Click(object sender, RoutedEventArgs e)
            {
                CheckBox cb = (CheckBox)e.OriginalSource;
                if (cb.IsChecked.HasValue)
                {
                    if ((bool)cb.IsChecked)
                    {
                        GridViewCell cell = (GridViewCell)cb.Parent;
                        GridViewRowItem gvri = cell.ParentRow;                                     
     
                        MessageBox.Show("Clicked");
                    }
                    else
                    {
     
                    }
                }
            }      

    Any pointers are appreciated.  Thanks.

    Sherri.

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 10 Oct 2011 Link to this post

    Hi Sherri,

    You could use the ParentOfType<T> extension method to find the corresponding row and get its Item property afterwards. For example:

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
            {
                GridViewRow parentRow = (sender as CheckBox).ParentOfType<GridViewRow>();
                if(parentRow != null)
                {
                    Club club = parentRow.Item as Club;
                    MessageBox.Show("The Name of the corresponding club is: " + club.Name);
                }
            }

    Let me know in case you need any further assistance.

    Greetings,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top