Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding a new row to a datagrid
Message
De
08/04/2013 10:46:54
 
 
À
08/04/2013 09:39:52
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01570195
Message ID:
01570388
Vues:
30
It turns out I needed to do this:
        private void grdAirWayBillDetails_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.grdAirWayBillDetails.CommitEdit();
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    AddDetailRow();
                }), DispatcherPriority.Background, null);
            }
        }
So now I just need to work out how to detect when I am on the last row and column in the grid.


>Ah. I 'm guessing that 'AirWayBillDetail' is a MM thing - in which case I don't know what it does so your understanding is better than mine :-}
>
>Doesn't sound from the syntax as if it is actually added to the collection tho :-{
>
>>if I understand it correctly, that's what this line does:
>>
>>
AirWayBillDetailEntity entity = this.AirWayBillDetail.NewEntity(new AirWayBillDetailDefaults(this.AirWayBill.Entity.awb_pk));
>>
>>The rest of it is setting focus to the correct cell in the grid.
>>
>>Or have I misunderstood?
>>
>>
>>>Haven't looked at this properly but would it make more sense (in AddDetailRow()) to just add the created entity to the collection to which the grid is bound ?
>>>
>>>>Anyone have any ideas on this?
>>>>
>>>>>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform