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

Getting Values from GridView with ComboBox in Template

1 Answer 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
IT
Top achievements
Rank 1
IT asked on 09 Oct 2011, 04:23 PM
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.

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 10 Oct 2011, 08:07 AM
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 >>

Tags
GridView
Asked by
IT
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or