Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding a new row to a datagrid
Message
De
05/04/2013 09:53:53
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Titre:
Adding a new row to a datagrid
Divers
Thread ID:
01570195
Message ID:
01570195
Vues:
51
Hi,

I have a datagrid with two columns, one a dropdown/combo and the other a textbox for entering a value. Once a value is entered in the last row I want to automatically add another row to the datagrid and place the focus on the combo.

This is the xaml for the grid:
                        <DataGrid AutoGenerateColumns="False" Grid.Row="1" Grid.Column="3" 
                                  Height="Auto" 
                                  HorizontalAlignment="Stretch" 
                                  Margin="0" 
                                  Name="grdAirWayBillDetails" 
                                  VerticalAlignment="Stretch" 
                                  Width="Auto" 
                                  IsSynchronizedWithCurrentItem="True" 
                                  ItemsSource="{Binding Mode=default}" 
                                  SelectionMode="Single" 
                                  GridLinesVisibility="None" 
                                  HeadersVisibility="Column" 
                                  Grid.ColumnSpan="2" 
                                 
                                  CanUserAddRows="False" 
                                  CanUserDeleteRows="False" 
                                  SelectionUnit="Cell" 
                                  Grid.RowSpan="4">
                            <DataGrid.Resources>
                                <Style x:Key="AlignRight" TargetType="DataGridCell">
                                    <Setter Property="HorizontalAlignment" Value="Right" />
                                    
                                </Style>
                                <Style x:Key="AlignRightHeader" TargetType="DataGridColumnHeader">
                                    <Setter Property="HorizontalAlignment" Value="Right" />
                                </Style>                            </DataGrid.Resources>
                            <DataGrid.Columns>
                                <DataGridComboBoxColumn Header="Charge Type" Width="2*" 
                                                        DisplayMemberPath="cty_name" 
                                                        SelectedValuePath="cty_pk" 
                                                        SelectedValueBinding="{Binding awd_ctyfk, Mode=Default}"/>

                                <DataGridTextColumn Header="Amount" Width="*" Binding="{Binding awd_amount, Mode=default}"
                                                    CellStyle="{StaticResource AlignRight}"
                                                    HeaderStyle="{StaticResource AlignRightHeader}"/>
                            </DataGrid.Columns>
                        </DataGrid>
This is my AddDetailRow() code:
        private void AddDetailRow()
        {
            AirWayBillDetailEntity entity = this.AirWayBillDetail.NewEntity(new AirWayBillDetailDefaults(this.AirWayBill.Entity.awb_pk));

            // Select cell and begin edit
            this.grdAirWayBillDetails.CommitEdit();

            this.grdAirWayBillDetails.Focus();
            System.Windows.Controls.DataGridCellInfo cellInfo = new System.Windows.Controls.DataGridCellInfo(entity, this.grdAirWayBillDetails.Columns[0]);
            this.grdAirWayBillDetails.SelectedCells.Clear();
            this.grdAirWayBillDetails.SelectedCells.Add(cellInfo);
            this.grdAirWayBillDetails.CurrentCell = cellInfo;
            this.grdAirWayBillDetails.BeginEdit();
        }
This code causes the total of the amount column in the grid to get calculated:
        void AirWayBillDetail_StateChange(mmBaseBusinessObject bizObj, mmBusinessStateChangeEventArgs e)
        {
            if (e.State == mmBusinessState.Retrieved)
            {
                // Store the Air WayBill Detail in the DataGrid's data context
                this.grdAirWayBillDetails.DataContext = this.AirWayBillDetail.EntityList;

                // Hide the new item row place holder in the Air WayBill Detail DataGrid
                ((IEditableCollectionView)CollectionViewSource.GetDefaultView(grdAirWayBillDetails.ItemsSource)).NewItemPlaceholderPosition =
                    NewItemPlaceholderPosition.None;
            }
            //this.ShowAirWayBillTotal();
            if (this.AirWayBillDetail.EntityList != null)
            {
                this.AirWayBillTotal = this.AirWayBillDetail.EntityList.Sum(x => x.awd_amount);
                foreach (var c in this.AirWayBillDetail.EntityList)
                {
                    c.PropertyChanged += CollectionItemChanged;
                }
            }

        }
This is what I have in my CollectionItemChanged:
        void CollectionItemChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "awd_amount")
            {
                AirWayBillTotal = AirWayBillDetail.EntityList.Sum(x => x.awd_amount);
                OnPropertyChanged("AirWayBillTotal");
                AddDetailRow();
            }
        }
What is happening now is that if I press Tab after entering an amount, a new row gets added, but focus goes to the next control in the tab order after the grid. So I have two questions:

1. How to check if I'm on the last row?

2. How to set focus to the combo?

update:

I have got a bit further with this using the PreviewKeyDown event and checking for Return, then calling my AddDetailRow(), but when I do this the combobox is not visible!
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform