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

Checkbox Click event not firing in nested grid

4 Answers 714 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ryan Bandouveres
Top achievements
Rank 1
Ryan Bandouveres asked on 23 Mar 2011, 06:26 PM
I have a RadGrid nested within another grid.  During the DataLoading event for the main outer grid, I have setup a RowLoaded event.  In this event, the click event for the checkboxes in the inner grid are wired to the appropriate method.  However, when I run the project the click event will not fire.  Any suggestions will be greatly appreciated.  Thank you.

<Window x:Class="FSPUpgradeServer.MainScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:fsp="clr-namespace:HarlandFS.FSP.Controls;assembly=FSPControls"
        Title="MainScreen"
        Height="500"
        Width="615">
    <Window.Resources>
        <Style TargetType="{x:Type telerik:GridViewRow}">
            <EventSetter Event="GotFocus"
                         Handler="GridViewRow_GotFocus"/>
        </Style>

    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="254*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="268*" />
            <RowDefinition Height="27*" />
            <RowDefinition Height="167*" />
        </Grid.RowDefinitions>
        <telerik:RadGridView Name="grdSystems"
                             AutoGenerateColumns="False"
                             IsFilteringAllowed="False"
                             ShowGroupPanel="False"
                             Grid.ColumnSpan="4"
                             Loaded="grdSystems_Loaded"
                             DataLoading="grdSystems_DataLoading">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Upgrade"
                                            IsReorderable="False"
                                            UniqueName="Upgrade">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Click="CheckBox_Click">
                            </CheckBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="System Name"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="SystemName" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="# of Accts"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfAccounts"
                                            TextAlignment="Right" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="# of Terminals"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfTerminals"
                                            TextAlignment="Right" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="# of msgs sent"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfMsgSent"
                                            TextAlignment="Right" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="# msgs received"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            TextAlignment="Right" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
        <Button Name="btnAdd"
                Grid.Row="1"
                Click="btnAdd_Click">_Add</Button>
        <Button Name="btnEdit"
                Grid.Row="1"
                Grid.Column="1"
                Click="btnEdit_Click">_Edit</Button>
        <Button Name="btnDelete"
                Grid.Row="1"
                Grid.Column="2"
                Click="btnDelete_Click">_Delete</Button>
        <GroupBox Grid.Row="2"
                  Grid.ColumnSpan="4">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="27*" />
                    <RowDefinition Height="27*" />
                    <RowDefinition Height="27*" />
                    <RowDefinition Height="27*" />
                    <RowDefinition Height="27*" />
                    <RowDefinition Height="27*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="133*" />
                    <ColumnDefinition Width="100*" />
                    <ColumnDefinition Width="133*" />
                    <ColumnDefinition Width="100*" />
                </Grid.ColumnDefinitions>
                <Label VerticalAlignment="Center">File Location</Label>
                <fsp:FSPTextBox Name="txtClientLocation"
                                Grid.Column="1"
                                Margin="2"
                                LostFocus="txtClientLocation_LostFocus"
                                IsRequired="True"
                                Text=".\FSP.msi"
                                Grid.ColumnSpan="2" />
                <Button Name="btnBrowse"
                        Grid.Column="3"
                        Click="btnBrowse_Click"
                        Margin="2">_Browse</Button>
                <Label Grid.Row="1"
                       VerticalAlignment="Center">Host Location</Label>
                <fsp:FSPTextBox Name="txtHostLocation"
                                Grid.Row="1"
                                Grid.Column="1"
                                Margin="2"
                                IsRequired="True"
                                Grid.ColumnSpan="2" />
                <Label Grid.Row="2"
                       VerticalAlignment="Center">Version Number</Label>
                <fsp:FSPTextBox Name="txtVersion"
                                Grid.Row="2"
                                Grid.Column="1"
                                Margin="2"
                                IsRequired="True" />
                <Label Grid.Row="2"
                       Grid.Column="2"
                       VerticalAlignment="Center">Messages to send</Label>
                <fsp:FSPTextBox Name="txtMessages"
                                Grid.Row="2"
                                Grid.Column="3"
                                Margin="2"
                                IsRequired="True"
                                DataType="Integer" />
                <Label Grid.Row="3"
                       VerticalAlignment="Center">Time Interval(Minutes)</Label>
                <fsp:FSPTextBox Name="txtTimeInterval"
                                Grid.Row="3"
                                Grid.Column="1"
                                Margin="2"
                                IsRequired="True"
                                DataType="Integer" />
                <Button Name="btnStart"
                        Grid.Row="3"
                        Grid.Column="3"
                        Click="btnStart_Click">_Start</Button>
                <Label Name="lblStatus"
                       Grid.Row="4"
                       Grid.ColumnSpan="4"
                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <ProgressBar Name="prgStatus"
                             Grid.Row="5"
                             Grid.ColumnSpan="4"
                             Margin="2" />
            </Grid>
        </GroupBox>
    </Grid>
</Window>

        public MainScreen()
        {
            InitializeComponent();

            messageTimer.Tick += new EventHandler(messageTimer_Tick);

            try
            {
                using (FileStream fs = new FileStream("FSPUpgradeAccounts.xml", FileMode.Open))
                {
                    SoapFormatter bf = new SoapFormatter();
                    ArrayList serializableList = bf.Deserialize(fs) as ArrayList;
                    //_AccountList = new ObservableCollection<FSPAccount>();

                    foreach (object item in serializableList)
                    {
                        FSPAccount addAccount = item as FSPAccount;
                        addAccount.GetTerminalList();
                        AccountList.Add(addAccount);
                    }
                }
            }
            catch
            {
            }
            //var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition();
            //detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Terminals");
            //grdAccounts.TableDefinition.ChildTableDefinitions.Add(detailDefinition);
            //grdAccounts.ItemsSource = AccountList;
            var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition();
            detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Accounts");
            grdSystems.TableDefinition.ChildTableDefinitions.Add(detailDefinition);
            grdSystems.ItemsSource = SystemList;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        private void grdSystems_DataLoading(object sender, GridViewDataLoadingEventArgs e)
        {
            var dataControl = (GridViewDataControl)sender;
            if (dataControl.ParentRow != null && dataControl.ChildTableDefinitions.Count == 0)
            {
                ObservableCollection<FSPAccount> account;
                try
                {
                    account = (ObservableCollection<FSPAccount>)e.ItemsSource;
                }
                catch
                {
                    //TODO:  Look for a better way to do this?
                    account = null;
                }
                if (account != null)
                {
                    //Setup relation to the Terminals sub grid
                    var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition();
                    detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Terminals");

                    //Create the columns for the Accounts "sub grid"
                    GridViewDataColumn column = new GridViewDataColumn();
                    column.UniqueName = "Upgrade";
                    GridViewCell cell = new GridViewCell();
                    
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "Name";
                    column.IsReadOnly = true;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "TotalTerminals";
                    column.Header = "Number of Terminals";
                    column.IsReadOnly = true;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "SentTerminals";
                    column.Header = "#Sent";
                    column.IsReadOnly = true;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "ReceivedTerminals";
                    column.Header = "#Received";
                    column.IsReadOnly = true;
                    dataControl.Columns.Add(column);
                    
                    //Set some properties on the Accounts sub grid
                    dataControl.ChildTableDefinitions.Add(detailDefinition);
                    dataControl.AutoGenerateColumns = false;
                    dataControl.ShowGroupPanel = false;
                    dataControl.IsFilteringAllowed = false;
                    dataControl.RowLoaded += new EventHandler<RowLoadedEventArgs>(dataControl_RowLoaded);
                    accountGridList.Add(dataControl);
                }
                else
                {
                    //Create columns for the Terminals sub grid
                    GridViewDataColumn column = new GridViewDataColumn();
                    column.UniqueName = "Name";
                    column.IsReadOnly = true;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "IsMessageSent";
                    column.Header = "Message Sent";
                    column.IsReadOnly = true;
                    dataControl.Columns.Add(column);
                    //dataControl.ChildTableDefinitions.Add(detailDefinition);

                    //Set some properties on the Terminals sub grid
                    dataControl.AutoGenerateColumns = false;
                    dataControl.ShowGroupPanel = false;
                    dataControl.IsFilteringAllowed = false;
                }
                //grdSystems.Rebind();
            }
        }

        void dataControl_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            if (e.Row is GridViewRow)
            {
                GridViewDataControl grid = (GridViewDataControl)sender;
                CheckBox chk = (CheckBox)e.Row.Cells[0].Content;
                chk.Click += new RoutedEventHandler(UpgradeAccount_Click);
            }
        }

        void UpgradeAccount_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("in Click");
        }

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 29 Mar 2011, 10:31 AM
Hi Ryan Bandouveres,

Currently you are subscribing to click events of the parent grid only. You can easily subscribe to the event of the child grids by using HierarchyChildTemplate to define you child grid.

I have attached an application that demonstrates which scenario. 


Best wishes,
Milan
the Telerik team
0
Ryan Bandouveres
Top achievements
Rank 1
answered on 29 Mar 2011, 04:04 PM
Thank-you, that really helped.  I got the click event to fire now, but I'm having a bit of trouble with refreshing data in the inner grid.  After checking the box in the parent grid, I iterate through my collections and update some properties on the data to which the Accounts (inner grid) grid is bound to.  However, I am not able to get the checkboxes in the inner grid to reflect the property changes I did when clicking the checkbox in the parent.  Thank you!

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckBox chk = (CheckBox)e.Source;
                GridViewCell cell = (GridViewCell)chk.Parent;
                //string systemName = cell.ParentRow.Cells[1].Content.ToString();
                object ParentCell = cell.ParentRow.DataContext;

                if (ParentCell is FSPSystem)
                {
                    //Checkbox in the System grid was checked
                    FSPSystem owningSystem = (FSPSystem)ParentCell;
                    string systemName = owningSystem.SystemName;

                    foreach (FSPSystem system in SystemList)
                    {
                        if (systemName == system.SystemName)
                        {
                            system.IsSelected = (bool)chk.IsChecked;
                            foreach (FSPAccount account in system.Accounts)
                            {
                                account.IsSelected = (bool)chk.IsChecked;
                            }
                        }
                        grdSystems.ChildTableDefinitions[0].DataSource = system.Accounts;
                    }
                    //grdSystems.Rebind();
                    //grdSystems.ItemsSource = SystemList;
                    //cell.ParentRow.GridViewDataControl.Rebind();
                    //grdSystems.ChildTableDefinitions[0].DataSource =
                    //foreach (GridViewDataControl ctl in accountGridList)
                    //{
                    //    ctl.Rebind();
                    //}
                }
                else
                {
                    //Checkbox in Accounts sub grid was clicked

                }
            }
            catch
            {
                //Catch and ignore
            }
        }


        <telerik:RadGridView Name="grdSystems"
                             AutoGenerateColumns="False"
                             IsFilteringAllowed="False"
                             ShowGroupPanel="False"
                             Grid.ColumnSpan="4"
                             Loaded="grdSystems_Loaded"
                             DataLoading="grdSystems_DataLoading"
                             CanUserDeleteRows="False"
                             CanUserInsertRows="False"
                             ActionOnLostFocus="CommitEdit"
                             CanUserFreezeColumns="False"
                             EditTriggers="Default">
            <telerik:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <telerik:RadGridView Name="grdAccounts"
                                         ItemsSource="{Binding Accounts}"
                                         ShowGroupPanel="False"
                                         IsFilteringAllowed="False"
                                         AutoGenerateColumns="False">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn Width="Auto"
                                                        Header="Select"
                                                        MinWidth="45"
                                                        UniqueName="IsSelected">
                                <telerik:GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Click="CheckBox_Click" />
                                        
                                    </DataTemplate>
                                </telerik:GridViewColumn.CellTemplate>
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"
                                                        IsReadOnly="True"
                                                        MinWidth="45">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding TotalTerminals}"
                                                        Header="Number of Terminals"
                                                        IsReadOnly="True"
                                                        MinWidth="125">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding SentTerminals}"
                                                        Header="Nbr Sent"
                                                        IsReadOnly="True"
                                                        MinWidth="55">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding ReceivedTerminals}"
                                                        Header="Nbr Received"
                                                        IsReadOnly="True"
                                                        MinWidth="85">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding HostVersion}"
                                                        Header="Host Version"
                                                        IsReadOnly="True"
                                                        MinWidth="100">
                            </telerik:GridViewDataColumn>

                        </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
                </DataTemplate>
            </telerik:RadGridView.HierarchyChildTemplate>
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Select"
                                            IsReorderable="False"
                                            MinWidth="45"
                                            UniqueName="IsSelected">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Click="CheckBox_Click">
                            </CheckBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="System Name"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            MinWidth="85"
                                            UniqueName="SystemName" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Nbr of Accts"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfAccounts"
                                            MinWidth="75"
                                            TextAlignment="Right" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Nbr of Terminals"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfTerminals"
                                            MinWidth="95"
                                            TextAlignment="Right" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Nbr of msgs sent"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfMsgSent"
                                            MinWidth="95"
                                            TextAlignment="Right" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Nbr msgs received"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            MinWidth="100"
                                            TextAlignment="Right" />
            </telerik:RadGridView.Columns>
           
        </telerik:RadGridView>
 
0
Milan
Telerik team
answered on 04 Apr 2011, 08:00 PM

Hi Ryan Bandouveres,

You shouldn't be working with UI elements to change values of you data items - you should work with the data directly.  For example, you should bind the IsSelected property of FSPSystem to the IsChecked property of the CheckBox - this could be done is pure XAML with no code behind, as demonstrated in the updated sample.. 



Kind 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
Ryan Bandouveres
Top achievements
Rank 1
answered on 05 Apr 2011, 07:41 PM
Thank you very much for the reply, but it is still not working correctly.  I tried to set it up based on your example, but I am not able to get the grid to bind to my collection when doing it all in xaml.  In the constructor for MainScreen, I set a watch on ItemSource for the grdSystems, and it is always reporting as being null.  Below you will find my updated xaml file and the code behind.  Thank you very much.

<Window x:Class="FSPUpgradeServer.MainScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:fsp="clr-namespace:HarlandFS.FSP.Controls;assembly=FSPControls"
        xmlns:local="clr-namespace:FSPUpgradeServer"
        Title="Release Management"
        Height="568"
        Width="654">
    <Window.Resources>
        <Style TargetType="{x:Type telerik:GridViewRow}">
            <EventSetter Event="GotFocus"
                         Handler="GridViewRow_GotFocus"/>
        </Style>
        <!--<Style TargetType="CheckBox">
            <EventSetter Event="Click"
                         Handler="UpgradeAccount_Click" />
        </Style>-->

    </Window.Resources>
    <Grid CheckBox.Click="Grid_Click">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="254*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="268*" />
            <RowDefinition Height="27*" MaxHeight="29"/>
            <RowDefinition Height="25*" />
            <RowDefinition Height="20*" />
            <RowDefinition Height="Auto"
                           MinHeight="42" />
            
        </Grid.RowDefinitions>
        <telerik:RadGridView Name="grdSystems"
                             AutoGenerateColumns="False"
                             IsFilteringAllowed="False"
                             ShowGroupPanel="False"
                             Grid.ColumnSpan="4"
                             DataLoading="grdSystems_DataLoading"
                             DataLoaded="grdSystems_DataLoaded"
                             CanUserDeleteRows="False"
                             CanUserInsertRows="False"
                             ActionOnLostFocus="CommitEdit"
                             CanUserFreezeColumns="False"
                             ItemsSource="{Binding ActiveState.SystemList}"
                             RowLoaded="grdSystems_RowLoaded">
            <telerik:RadGridView.ChildTableDefinitions>
                <telerik:GridViewTableDefinition />
            </telerik:RadGridView.ChildTableDefinitions>
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Select"
                                            IsReorderable="False"
                                            MinWidth="45"
                                            UniqueName="IsSelected"
                                            DataMemberBinding="{Binding IsSelected, Mode=TwoWay}">
                    <!--<telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Click="CheckBox_Click"
                                      Checked="CheckBox_Checked"
                                      IsThreeState="True">
                            </CheckBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>-->
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="System Name"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            MinWidth="85"
                                            DataMemberBinding="{Binding SystemName}" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Total Accts"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            UniqueName="NumberOfAccounts"
                                            MinWidth="75"
                                            TextAlignment="Right"
                                            DataMemberBinding="{Binding NumberOfAccounts}" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Total Terminals"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            MinWidth="95"
                                            TextAlignment="Right"
                                            DataMemberBinding="{Binding NumberOfTerminals}" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Total Msgs Sent"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            MinWidth="95"
                                            TextAlignment="Right"
                                            DataMemberBinding="{Binding NumberOfMsgSent}" />
                <telerik:GridViewDataColumn Width="Auto"
                                            Header="Total Msgs Received"
                                            IsReadOnly="True"
                                            IsReorderable="False"
                                            MinWidth="100"
                                            TextAlignment="Right"
                                            DataMemberBinding="{Binding NumberOfMsgReceived}" />
            </telerik:RadGridView.Columns>

            <telerik:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <telerik:RadGridView Name="grdAccounts"
                                         ItemsSource="{Binding Accounts}"
                                         ShowGroupPanel="False"
                                         IsFilteringAllowed="False"
                                         AutoGenerateColumns="False">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn Width="Auto"
                                                        Header="Select"
                                                        MinWidth="45"
                                                        DataMemberBinding="{Binding IsSelected, Mode=TwoWay}">
                                <!--<telerik:GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Click="CheckBox_Click"/>
                                    </DataTemplate>
                                </telerik:GridViewColumn.CellTemplate>-->
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"
                                                        IsReadOnly="True"
                                                        Header="Account Name"
                                                        MinWidth="65">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding TotalTerminals}"
                                                        Header="Number of Terminals"
                                                        IsReadOnly="True"
                                                        MinWidth="125">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding SentTerminals}"
                                                        Header="Total Msgs Sent"
                                                        IsReadOnly="True"
                                                        MinWidth="55">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding ReceivedTerminals}"
                                                        Header="Total Msgs Received"
                                                        IsReadOnly="True"
                                                        MinWidth="85">
                            </telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding HostVersion}"
                                                        Header="Host Version"
                                                        IsReadOnly="True"
                                                        MinWidth="100">
                            </telerik:GridViewDataColumn>

                        </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
                </DataTemplate>
            </telerik:RadGridView.HierarchyChildTemplate>
           
        </telerik:RadGridView>
        <Button Name="btnAdd"
                Grid.Row="1"
                Click="btnAdd_Click">_Add</Button>
        <Button Name="btnEdit"
                Grid.Row="1"
                Grid.Column="1"
                Click="btnEdit_Click">_Edit</Button>
        <!--<Button Name="btnEdit"
                Grid.Row="1"
                Grid.Column="1"
                Command="local:CustomCommands.EditCommand">_Edit</Button>-->
        <Button Name="btnDelete"
                Grid.Row="1"
                Grid.Column="2"
                Click="btnDelete_Click">_Delete</Button>
        <!--<GroupBox Grid.Row="2"
                  Grid.ColumnSpan="4">-->
            <Expander Grid.Row="4"
                      Margin="2"
                      Name="ExpSettings"
                      Grid.ColumnSpan="4"
                      IsExpanded="True">
                <Expander.Header>
                    <TextBlock TextWrapping="NoWrap">Settings</TextBlock>
                </Expander.Header>
                <Expander.Content>
                    <GroupBox Header="Upgrade Notification Settings"
                              Margin="2"
                              Background="White"
                              FontWeight="Bold">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="35*" MinHeight="30"/>
                                <RowDefinition Height="35*" MinHeight="30"/>
                                <RowDefinition Height="35*" MinHeight="30"/>
                                <RowDefinition Height="35*" MinHeight="30"/>
                                <RowDefinition Height="35*" MinHeight="30"/>
                        </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="133*" />
                                <ColumnDefinition Width="100*" />
                                <ColumnDefinition Width="133*" />
                                <ColumnDefinition Width="100*" />
                            </Grid.ColumnDefinitions>
                            <Label VerticalAlignment="Center" FontWeight="Normal">File Location</Label>
                            <fsp:FSPTextBox Name="txtClientLocation"
                                            Grid.Column="1"
                                            Margin="2"
                                            LostFocus="txtClientLocation_LostFocus"
                                            IsRequired="True"
                                            Text=".\FSP.msi"
                                            FontWeight="Normal"
                                            Grid.ColumnSpan="2" />
                            <Button Name="btnBrowse"
                                    Grid.Column="3"
                                    FontWeight="Normal"
                                    Click="btnBrowse_Click"
                                    Margin="2">_Browse</Button>
                            <Label Grid.Row="1"
                                   VerticalAlignment="Center" FontWeight="Normal">Host Location</Label>
                            <fsp:FSPTextBox Name="txtHostLocation"
                                            Grid.Row="1"
                                            Grid.Column="1"
                                            Margin="2"
                                            IsRequired="True"
                                            FontWeight="Normal"
                                            Grid.ColumnSpan="2" />
                            <Label Grid.Row="2"
                                   VerticalAlignment="Center" FontWeight="Normal">Version Number</Label>
                            <fsp:FSPTextBox Name="txtVersion"
                                            FontWeight="Normal"
                                            Grid.Row="2"
                                            Grid.Column="1"
                                            Margin="2"
                                            IsLocked="True"
                                            IsRequired="True" />
                            <Label Grid.Row="3"
                                   VerticalAlignment="Center" FontWeight="Normal">Time Interval (Minutes)</Label>
                            <fsp:FSPTextBox Name="txtTimeInterval"
                                            Grid.Row="3"
                                            Grid.Column="1"
                                            Margin="2"
                                            IsRequired="True"
                                            FontWeight="Normal"
                                            DataType="Integer" />
                            <Label Grid.Row="4"
                                   Grid.Column="0"
                                   Margin="2"
                                   FontWeight="Normal"
                                   VerticalAlignment="Center">Message Increment</Label>
                            <fsp:FSPTextBox Name="txtMessages"
                                            Grid.Row="4"
                                            Grid.Column="1"
                                            Margin="2"
                                            IsRequired="True"
                                            FontWeight="Normal"
                                            DataType="Integer" />
                        <Button Name="btnStart"
                                    Grid.Row="3"
                                    Grid.Column="3"
                                    FontWeight="Normal"
                                    Click="btnStart_Click">_Start</Button>
                            <Button Name="btnOpen"
                                    Grid.Row="2"
                                    Grid.Column="3"
                                    FontWeight="Normal"
                                    Click="btnOpen_Click">_Open</Button>

                        <Button Name="btnSave"
                                    Grid.Row="4"
                                    Grid.Column="3"
                                    FontWeight="Normal"
                                    Click="btnSave_Click">Save Schedule</Button>

                        <!--<ProgressBar Name="prgStatus"
                                         Grid.Row="5"
                                         Grid.ColumnSpan="4"
                                         Margin="2" />-->
                        </Grid>
                    </GroupBox>
                </Expander.Content>
            </Expander>
        <Label Name="lblStatus"
               Grid.Row="2"
               Grid.ColumnSpan="4"
               HorizontalAlignment="Center"
               FontWeight="Normal"
               VerticalAlignment="Center" />

        <ProgressBar Name="prgStatus"
                     Grid.Row="3"
                     Grid.ColumnSpan="4"
                     Margin="2" />

        <!--</GroupBox>-->
    </Grid>
</Window>

using HarlandFS.FSP.Utilities;
using HarlandFS.FSP.MsgUtilities;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Data;
using WindowsInstaller;


namespace FSPUpgradeServer
{
    /// <summary>
    /// Interaction logic for MainScreen.xaml
    /// </summary>
    public partial class MainScreen : Window
    {

        #region Private Fields/Properties

        private static Action EmptyDelegate = delegate() { };


        private bool isStarted = false;

        private FSPAccount connectedAccount = null;

        private Queue<FSPTerminal> MessageList = new Queue<FSPTerminal>();

        private DispatcherTimer messageTimer = new DispatcherTimer();

        private UPGSettings ServerSettings = new UPGSettings();
        //private Status statusWindow = null;
        #endregion

        #region Public Static Properties
        #endregion

        #region Public Instance Properties

        private SaveState _ActiveState = new SaveState();
        public SaveState ActiveState
        {
            get
            {
                return _ActiveState;
            }
            set
            {
                _ActiveState = value;
            }
        }
        //TODO:  Anywhere that SystemList is being used, replace that with ActiveState.SystemList

        private ObservableCollection<FSPAccount> _AccountList = new ObservableCollection<FSPAccount>();
        public ObservableCollection<FSPAccount> AccountList
        {
            get { return _AccountList; }
        }

        //private ObservableCollection<FSPSystem> _SystemList = new ObservableCollection<FSPSystem>();
        //public ObservableCollection<FSPSystem> SystemList
        //{
        //    get
        //    {
        //        return _SystemList;
        //    }
        //}
        private List<GridViewDataControl> accountGridList = new List<GridViewDataControl>();

        #endregion

        #region Constructors/Destructors

        public MainScreen()
        {
            InitializeComponent();
            //grdSystems.ItemsSource = ActiveState.SystemList;
            ActiveState.Settings = ServerSettings;
            //ActiveState.SystemList = SystemList;
            messageTimer.Tick += new EventHandler(messageTimer_Tick);
            DeSerializeData();
            SetupScreen();
            
            //Keep below for reference for how to use "Commands"

            //CommandBinding abinding = new CommandBinding();
            //abinding.Command = CustomCommands.EditCommand;
            //abinding.Executed += new ExecutedRoutedEventHandler(Edit_Handler);
            //abinding.CanExecute += new CanExecuteRoutedEventHandler(Edit_EnabledHandler);
            //this.CommandBindings.Add(abinding);
        }

        public MainScreen(bool DoDeserialize)
        {
            InitializeComponent();

            if (DoDeserialize == true)
            {
                DeSerializeData();
            }
            SetupScreen();
        }

        private void SetupScreen()
        {
            //Bind all the settings fields to their corresponding properties in UPGSettings.cs.
            //ActiveState.Settings = ServerSettings;
            Binding binding = new Binding();
            binding.Source = ServerSettings;
            binding.Path = new PropertyPath("FileLocation");
            txtClientLocation.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ServerSettings;
            binding.Path = new PropertyPath("HostLocation");
            txtHostLocation.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ServerSettings;
            binding.Path = new PropertyPath("VersionNumber");
            txtVersion.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ServerSettings;
            binding.Path = new PropertyPath("Interval");
            txtTimeInterval.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ServerSettings;
            binding.Path = new PropertyPath("Increment");
            txtMessages.SetBinding(TextBox.TextProperty,binding);

            //var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition();
            //detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Accounts");
            //grdSystems.TableDefinition.ChildTableDefinitions.Add(detailDefinition);
            //grdSystems.ItemsSource = ActiveState.SystemList;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }
        private void DeSerializeData()
        {
            try
            {
                using (FileStream fs = new FileStream("FSPUpgradeAccounts.xml", FileMode.Open))
                {
                    SoapFormatter bf = new SoapFormatter();
                    ArrayList serializableList = bf.Deserialize(fs) as ArrayList;
                    //_AccountList = new ObservableCollection<FSPAccount>();

                    foreach (object item in serializableList)
                    {
                        FSPAccount addAccount = item as FSPAccount;
                        addAccount.GetTerminalList();
                        AccountList.Add(addAccount);
                    }
                }
            }
            catch
            {
            }

        }
        #endregion

        #region Public Static Methods
        #endregion

        #region Public Instance Methods
        #endregion

        #region Private Methods

        private void saveAccountList()
        {
            ArrayList serializableList = new ArrayList();

            foreach (FSPAccount item in AccountList)
            {
                serializableList.Add(item);
            }

            using (FileStream fs = new FileStream("FSPUpgradeAccounts.xml", FileMode.Create))
            {
                SoapFormatter bf = new SoapFormatter();
                bf.Serialize(fs, serializableList);
            }

        }

        #endregion

        #region Interface Members
        #endregion

        #region Events and Delegates

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

            EditAccount editForm = new EditAccount();
            if ((bool) editForm.ShowDialog())
            {
                editForm.Account.GetTerminalList();
                AccountList.Add(editForm.Account);
                bool found = false;
                foreach (FSPSystem system in ActiveState.SystemList)
                {
                    if (editForm.Account.IPAddress == system.SystemName)
                    {
                        system.Accounts.Add(editForm.Account);
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    FSPSystem newSystem = new FSPSystem(editForm.Account.IPAddress);
                    newSystem.Accounts.Add(editForm.Account);
                    ActiveState.SystemList.Add(newSystem);
                }
                grdSystems.Rebind();
                //TODO:  Change to saveSystemList()
                //saveAccountList();
            }
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            // Configure open file dialog box
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = txtClientLocation.Text; // Default file name
            dlg.DefaultExt = ".msi"; // Default file extension
            dlg.Filter = "Windows Installer Package (.msi)|*.msi"; // Filter files by extension

            // Show open file dialog box
            bool? result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                txtClientLocation.Text = dlg.FileName;
                txtClientLocation.Validate();
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            int numSelected = 0;

            foreach (FSPSystem system in ActiveState.SystemList)
            {
                foreach (FSPAccount account in system.Accounts)
                {
                    if (account.IsSelected == true)
                    {
                        numSelected++;
                    }
                }
            }
            if (numSelected == 0)
            {
                MessageBox.Show("Please select an account to delete.", "Upgrade Server", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                MessageBoxResult answer = MessageBox.Show("Are you sure you wish to delete the " + numSelected.ToString() + " selected account(s)?", "", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (answer == MessageBoxResult.Yes)
                {
                    for (int i = ActiveState.SystemList.Count-1; i > -1; i--)
                    {
                        for (int j = ActiveState.SystemList[i].Accounts.Count - 1; j > -1; j--)
                        {
                            if (ActiveState.SystemList[i].Accounts[j].IsSelected == true)
                            {
                                ActiveState.SystemList[i].Accounts.Remove(ActiveState.SystemList[i].Accounts[j]);
                            }
                        }
                        if (ActiveState.SystemList[i].Accounts.Count == 0)
                        {
                            ActiveState.SystemList.Remove(ActiveState.SystemList[i]);
                        }
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            //TODO:  Fix edit to work with new grid
            //EditAccount editForm = new EditAccount();
            //editForm.Account = (FSPAccount) grdAccounts.SelectedItem;
            //editForm.ShowDialog();
        }

        private void Edit_Handler(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Edit");
        }

        private void Edit_EnabledHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = grdSystems.SelectedItems.Count > 0;
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (isStarted)
            {
                messageTimer.Stop();
                //statusWindow.Hide();
                //statusWindow = null;
                btnAdd.IsEnabled = true;
                btnEdit.IsEnabled = true;
                btnDelete.IsEnabled = true;
                btnBrowse.IsEnabled = true;
                txtClientLocation.IsLocked = false;
                txtHostLocation.IsLocked = false;
                txtMessages.IsLocked = false;
                txtTimeInterval.IsLocked = false;
                txtVersion.IsLocked = false;
                btnStart.Content = "_Start";
                isStarted = false;
            }
            else
            {
                txtClientLocation.Validate();
                txtHostLocation.Validate();
                txtVersion.Validate();
                txtMessages.Validate();
                txtTimeInterval.Validate();

                if (!txtClientLocation.IsValid)
                {
                    MessageBox.Show(txtClientLocation.ValidationError);
                    txtClientLocation.Focus();
                    return;
                }

                if (!txtHostLocation.IsValid)
                {
                    MessageBox.Show(txtHostLocation.ValidationError);
                    txtHostLocation.Focus();
                    return;
                }

                if (!txtVersion.IsValid)
                {
                    MessageBox.Show(txtVersion.ValidationError);
                    txtVersion.Focus();
                    return;
                }

                if (!txtMessages.IsValid)
                {
                    MessageBox.Show(txtMessages.ValidationError);
                    txtMessages.Focus();
                    return;
                }

                if (!txtTimeInterval.IsValid)
                {
                    MessageBox.Show(txtTimeInterval.ValidationError);
                    txtTimeInterval.Focus();
                    return;
                }

                string fromFile = txtClientLocation.Text;
                string[] fromFileName = fromFile.Split('\\');
                string toFile = txtHostLocation.Text;
                string[] toFileName = toFile.Split('/');
                if (fromFileName[fromFileName.Length - 1] != toFileName[toFileName.Length - 1])
                {
                    if (txtHostLocation.Text.Substring(txtHostLocation.Text.Length - 1) != "/")
                    {
                        txtHostLocation.Text += "/";
                    }
                    txtHostLocation.Text += fromFileName[fromFileName.Length - 1];
                }

                MessageList.Clear();
                foreach (FSPAccount account in AccountList)
                {
                    foreach (FSPTerminal terminal in account.Terminals)
                    {
                        //terminal.IsMessageSent = false;
                        terminal.TimeSent = null;
                        MessageList.Enqueue(terminal);
                    }
                }

                btnAdd.IsEnabled = false;
                btnEdit.IsEnabled = false;
                btnDelete.IsEnabled = false;
                btnBrowse.IsEnabled = false;
                txtClientLocation.IsLocked = true;
                txtHostLocation.IsLocked = true;
                txtMessages.IsLocked = true;
                txtTimeInterval.IsLocked = true;
                txtVersion.IsLocked = true;
                isStarted = true;
                btnStart.Content = "_Stop";
                prgStatus.Maximum = MessageList.Count;
                //statusWindow = new Status(MessageList.Count);
                //statusWindow.Show();
                //statusWindow.setStatus("Waiting for next interval: " + MessageList.Count.ToString() + " messages left to send.", null);
                prgStatus.Value = 0;
                lblStatus.Content = "Waiting for next interval: " + MessageList.Count.ToString() + " messages left to send.";
                messageTimer.Interval = new TimeSpan(0, 0, 1);
                messageTimer.Start();
            }
        }

        void messageTimer_Tick(object sender, EventArgs e)
        {
            if (messageTimer.Interval.Seconds == 1)
            {
                messageTimer.Interval = new TimeSpan(0, Convert.ToInt32(txtTimeInterval.Text), 0);    
            }

            for (int messageCount = 0; messageCount < Convert.ToInt32(txtMessages.Text); messageCount++)
            {
                if (MessageList.Count > 0)
                {
                    FSPTerminal terminal = MessageList.Dequeue();

                    if (terminal.Account != connectedAccount)
                    {
                        if (connectedAccount != null)
                        {
                            connectedAccount.CloseConnection();
                        }

                        connectedAccount = terminal.Account;
                        lblStatus.Content = "Sending the upgrade file for Account " + connectedAccount.Name;
                        Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
                        connectedAccount.OpenConnection();

                        if (!connectedAccount.UploadFile(txtClientLocation.Text, txtHostLocation.Text))
                        {
                            connectedAccount.CloseConnection();
                        }
                    }
                    terminal.SendMessage(txtVersion.Text + FSPStandards.VM + txtHostLocation.Text);
                    //statusWindow.setStatus("Waiting for next interval: " + MessageList.Count.ToString() + " messages left to send.", 1);
                    lblStatus.Content = "Waiting for next interval: " + MessageList.Count.ToString() + " messages left to send.";
                    prgStatus.Value += 1;
                    //grdAccounts.Rebind();
                    grdSystems.Rebind();
                }
                else
                {
                    //grdAccounts.Rebind();
                    grdSystems.Rebind();
                    lblStatus.Content = "All messages have been sent.";
                    btnStart_Click(this, new RoutedEventArgs());
                    return;
                }
            }
        }

        private void txtClientLocation_LostFocus(object sender, RoutedEventArgs e)
        {
            if (txtClientLocation.IsValid)
            {
                FileInfo installFile = new FileInfo(txtClientLocation.Text);

                if (!installFile.Exists || installFile.Extension != ".msi")
                {
                    txtClientLocation.ValidationMessage = ("The install file to distribute is not valid.");
                }
                else
                {
                    //Extract the Version Number from the msi and populate the Version Number field with it.

                    //Get the type of the Windows Installer object
                    Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

                    //Create the Windows Install object
                    Installer installer = (Installer)Activator.CreateInstance(installerType);

                    //Open the MSI database in the input file
                    Database database = installer.OpenDatabase(txtClientLocation.Text, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);

                    //Open a view on the Property table for the version property
                    View view = database.OpenView("SELECT * FROM Property WHERE Property = 'FSP_VERSION'");

                    //Execute the view query
                    view.Execute(null);

                    //Get the record from the view
                    Record record = view.Fetch();

                    //Get the version from the data
                    string version = record.get_StringData(2);
                    txtVersion.Text = version;
                    txtVersion.Validate();
                }
            }
        }

        //private void grdAccounts_Loaded(object sender, RoutedEventArgs e)
        //{

        //}

        //private void grdAccounts_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e)
        //{
        //    var dataControl = (GridViewDataControl)sender;
        //    if (dataControl.ParentRow != null && dataControl.ChildTableDefinitions.Count == 0)
        //    {
        //        var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition();
        //        detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Terminals");
        //        GridViewDataColumn column = new GridViewDataColumn();
        //        column.UniqueName = "IsMessageSent";
        //        dataControl.Columns.Add(column);
        //        column = new GridViewDataColumn();
        //        column.UniqueName = "Name";
        //        dataControl.Columns.Add(column);
        //        //grdAccounts.TableDefinition.ChildTableDefinitions.Add(detailDefinition);
        //        dataControl.ChildTableDefinitions.Add(detailDefinition);
        //        dataControl.AutoGenerateColumns = false;
        //        dataControl.ShowGroupPanel = false;
        //        dataControl.IsFilteringAllowed = false;
        //        //grdAccounts.ItemsSource = AccountList;
        //        //grdAccounts.Rebind();

        //        //dataControl.DataLoading += new EventHandler<GridViewDataLoadingEventArgs>(dataControl_DataLoading);
        //        //dataControl.ChildTableDefinitions.Add(new GridViewTableDefinition() { Relation = new PropertyRelation("Terminals") });
        //        //dataControl.ChildTableDefinitions.
        //    }

        //}


        private void grdSystems_DataLoading(object sender, GridViewDataLoadingEventArgs e)
        {
            var dataControl = (GridViewDataControl)sender;

            if (dataControl.ParentRow != null && dataControl.ChildTableDefinitions.Count == 0)
            {
                ObservableCollection<FSPAccount> account;
                try
                {
                    account = (ObservableCollection<FSPAccount>)e.ItemsSource;
                }
                catch
                {
                    //TODO:  Look for a better way to do this?
                    account = null;
                }
                if (account != null)
                {
                    //if (dataControl.ParentRow.Item is FSPSystem)
                    //{
                    //    string systemName = (String)dataControl.ParentRow.Cells[1].Content;
                    //    dataControl.ParentRow.IsSelected = true;
                    //}
                    //Setup relation to the Terminals sub grid
                    var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition();
                    detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Terminals");

                    //Create the columns for the Accounts "sub grid"
                    //GridViewDataColumn column = new GridViewDataColumn();
                    //column.UniqueName = "IsSelected";
                    //column.Header = "Select";
                    ////GridViewCell cell = new GridViewCell();
                    //column.MinWidth = 45;
                    //dataControl.Columns.Add(column);
                    //column = new GridViewDataColumn();
                    //column.UniqueName = "Name";
                    //column.IsReadOnly = true;
                    //column.MinWidth = 45;
                    //dataControl.Columns.Add(column);
                    //column = new GridViewDataColumn();
                    //column.UniqueName = "TotalTerminals";
                    //column.Header = "Number of Terminals";
                    //column.IsReadOnly = true;
                    //column.MinWidth = 125;
                    //dataControl.Columns.Add(column);
                    //column = new GridViewDataColumn();
                    //column.UniqueName = "SentTerminals";
                    //column.Header = "Nbr Sent";
                    //column.IsReadOnly = true;
                    //column.MinWidth = 55;
                    //dataControl.Columns.Add(column);
                    //column = new GridViewDataColumn();
                    //column.UniqueName = "ReceivedTerminals";
                    //column.Header = "Nbr Received";
                    //column.IsReadOnly = true;
                    //column.MinWidth = 85;
                    //dataControl.Columns.Add(column);
                    //column = new GridViewDataColumn();
                    //column.UniqueName = "HostVersion";
                    //column.Header = "Host Version";
                    //column.IsReadOnly = true;
                    //column.MinWidth = 100;
                    //dataControl.Columns.Add(column);
                    ////Set some properties on the Accounts sub grid
                    dataControl.ChildTableDefinitions.Add(detailDefinition);
                    //dataControl.ItemsSource = "{Binding Accounts}";
                    //dataControl.AutoGenerateColumns = false;
                    //dataControl.ShowGroupPanel = false;
                    //dataControl.IsFilteringAllowed = false;
                    //dataControl.RowLoaded += new EventHandler<RowLoadedEventArgs>(dataControl_RowLoaded);
                    //dataControl.CurrentCellChanged += new EventHandler<GridViewCurrentCellChangedEventArgs>(dataControl_CurrentCellChanged);
                    if (dataControl.DataContext is FSPSystem)
                    {
                        //Avoid adding duplicate DataControl objects into the collection
                        string systemName = ((FSPSystem)dataControl.DataContext).SystemName;
                        for (int i = 0; i < accountGridList.Count; i++)
                        {
                            if (accountGridList[i].DataContext is FSPSystem)
                            {
                                if (systemName == ((FSPSystem)accountGridList[i].DataContext).SystemName)
                                {
                                    accountGridList.Remove(accountGridList[i]);
                                    break;
                                }
                            }
                        }
                    }
                    accountGridList.Add(dataControl);
                }
                else
                {
                    //Create columns for the Terminals sub grid
                    GridViewDataColumn column = new GridViewDataColumn();
                    column.UniqueName = "Name";
                    column.Header = "Terminal Name";
                    column.IsReadOnly = true;
                    column.MinWidth = 95;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "TimeSent";
                    column.Header = "Time Sent";
                    column.IsReadOnly = true;
                    column.MinWidth = 75;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "TimeReceived";
                    column.Header = "Time Received";
                    column.IsReadOnly = true;
                    column.MinWidth = 90;
                    dataControl.Columns.Add(column);
                    column = new GridViewDataColumn();
                    column.UniqueName = "ElapsedTime";
                    column.Header = "Elapsed Time(HH:MM:SS)";
                    column.IsReadOnly = true;
                    column.MinWidth = 140;
                    dataControl.Columns.Add(column);
                    //dataControl.ChildTableDefinitions.Add(detailDefinition);

                    //Set some properties on the Terminals sub grid
                    dataControl.AutoGenerateColumns = false;
                    dataControl.ShowGroupPanel = false;
                    dataControl.IsFilteringAllowed = false;
                    if (dataControl.DataContext is FSPAccount)
                    {
                        //Avoid adding duplicate DataControl objects into the collection
                        string accountName = ((FSPAccount)dataControl.DataContext).Name;
                        for (int i = 0; i < accountGridList.Count; i++)
                        {
                            if (accountGridList[i].DataContext is FSPAccount)
                            {
                                accountGridList.Remove(accountGridList[i]);
                                break;
                            }
                        }
                    }
                    accountGridList.Add(dataControl);
                }
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            //try
            //{
            //    CheckBox chk = (CheckBox)e.Source;
            //    GridViewCell cell = (GridViewCell)chk.Parent;
            //    //string systemName = cell.ParentRow.Cells[1].Content.ToString();
            //    object ParentCell = cell.ParentRow.DataContext;

            //    if (ParentCell is FSPSystem)
            //    {
            //        //Checkbox in the System grid was checked
            //        FSPSystem owningSystem = (FSPSystem)ParentCell;
            //        string systemName = owningSystem.SystemName;

            //        foreach (FSPSystem system in ActiveState.SystemList)
            //        {
            //            if (systemName == system.SystemName)
            //            {
            //                system.IsSelected = chk.IsChecked;
            //                foreach (FSPAccount account in system.Accounts)
            //                {
            //                    account.IsSelected = (bool)chk.IsChecked;
            //                    //for (int i = 0; i < accountGridList.Count; i++)
            //                    //{
            //                    //    if (accountGridList[i].DataContext is FSPAccount)
            //                    //    {
            //                    //        if (((FSPAccount)accountGridList[i].DataContext).Name == account.Name)
            //                    //        {
            //                    //            ((FSPAccount)accountGridList[i].DataContext).IsSelected = account.IsSelected;
            //                    //        }
            //                    //    }
            //                    //}
            //                }
            //            }
            //        }
            //        foreach (GridViewDataControl ctl in accountGridList)
            //        {
            //            //if (ctl.DataContext is FSPAccount)
            //            //{
            //                ctl.Rebind();
            //            //}
            //        }
            //    }
            //    else
            //    {
            //        //Checkbox in Accounts sub grid was clicked

            //    }
            //}
            //catch
            //{
            //    //Catch and ignore
            //}
        }

        private void GridViewRow_GotFocus(object sender, RoutedEventArgs e)
        {
            if (sender is GridViewRow)
            {
                GridViewRow row = (GridViewRow)sender;
                row.IsSelected = true;
                e.Handled = true;
            }
        }

        private void Grid_Click(object sender, RoutedEventArgs e)
        {
            //if (e.Source is RadGridView && e.OriginalSource is CheckBox)
            //{
            //    RadGridView grid = (RadGridView)e.Source;
            //    FSPSystem system = (FSPSystem)grid.SelectedItem;
            //    CheckBox chk = (CheckBox)e.OriginalSource;
            //    if (grid.SelectedItem != null)
            //    {
            //        if (chk.DataContext is FSPAccount)
            //        {
            //            FSPAccount account = (FSPAccount)chk.DataContext;
            //            account.IsSelected = (bool)chk.IsChecked;
            //        }
            //        bool allChecked = true;
            //        foreach (FSPAccount account in system.Accounts)
            //        {
            //            if (account.IsSelected == false)
            //            {
            //                allChecked = false;
            //                break;
            //            }
            //        }
            //        if (allChecked == false)
            //        {
            //            system.IsSelected = null;
            //        }
            //        else
            //        {
            //            system.IsSelected = true;
            //        }
            //        foreach (GridViewDataControl ctl in accountGridList)
            //        {
            //            if (ctl.DataContext is FSPSystem)
            //            {
            //                ctl.Rebind();
            //            }
            //        }
            //    }
            //}
        }

        private void grdSystems_DataLoaded(object sender, EventArgs e)
        {
            //var dataControl = (GridViewDataControl)sender;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            MainScreen screen = new MainScreen(false);
            screen.ExpSettings.IsExpanded = false;
            screen.btnAdd.Visibility = Visibility.Hidden;
            screen.btnEdit.Visibility = Visibility.Hidden;
            screen.btnDelete.Visibility = Visibility.Hidden;
            screen.btnBrowse.Visibility = Visibility.Hidden;
            screen.btnOpen.Visibility = Visibility.Hidden;
            screen.btnSave.Visibility = Visibility.Hidden;
            screen.btnStart.Visibility = Visibility.Hidden;
            screen.txtClientLocation.IsLocked = true;
            screen.txtHostLocation.IsLocked = true;
            screen.txtVersion.IsLocked = true;
            screen.txtTimeInterval.IsLocked = true;
            screen.txtMessages.IsLocked = true;
            screen.Show();
        }


        #endregion

        private void grdSystems_RowLoaded(object sender, RowLoadedEventArgs e)
        {

        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //try
            //{
            //    CheckBox chk = (CheckBox)e.Source;
            //    GridViewCell cell = (GridViewCell)chk.Parent;
            //    //string systemName = cell.ParentRow.Cells[1].Content.ToString();
            //    object ParentCell = cell.ParentRow.DataContext;

            //    if (ParentCell is FSPSystem)
            //    {
            //        //Checkbox in the System grid was checked
            //        FSPSystem owningSystem = (FSPSystem)ParentCell;
            //        string systemName = owningSystem.SystemName;

            //        foreach (FSPSystem system in ActiveState.SystemList)
            //        {
            //            if (systemName == system.SystemName)
            //            {
            //                system.IsSelected = chk.IsChecked;
            //                foreach (FSPAccount account in system.Accounts)
            //                {
            //                    account.IsSelected = (bool)chk.IsChecked;
            //                    //for (int i = 0; i < accountGridList.Count; i++)
            //                    //{
            //                    //    if (accountGridList[i].DataContext is FSPAccount)
            //                    //    {
            //                    //        if (((FSPAccount)accountGridList[i].DataContext).Name == account.Name)
            //                    //        {
            //                    //            ((FSPAccount)accountGridList[i].DataContext).IsSelected = account.IsSelected;
            //                    //        }
            //                    //    }
            //                    //}
            //                }
            //            }
            //        }
            //        foreach (GridViewDataControl ctl in accountGridList)
            //        {
            //            //if (ctl.DataContext is FSPAccount)
            //            //{
            //            ctl.Rebind();
            //            //}
            //        }
            //    }
            //    else
            //    {
            //        //Checkbox in Accounts sub grid was clicked

            //    }
            //}
            //catch
            //{
            //    //Catch and ignore
            //}

        }

    }
}

Tags
GridView
Asked by
Ryan Bandouveres
Top achievements
Rank 1
Answers by
Milan
Telerik team
Ryan Bandouveres
Top achievements
Rank 1
Share this question
or