Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding a new row to a datagrid
Message
From
08/04/2013 12:58:43
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01570195
Message ID:
01570403
Views:
40
:)

The full code (including checking for the last row and column):
        private void grdAirWayBillDetails_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && this.grdAirWayBillDetails.CurrentCell.Column.Header.ToString() == "Amount" 
                && this.grdAirWayBillDetails.Items[this.grdAirWayBillDetails.Items.Count -1] == this.grdAirWayBillDetails.CurrentCell.Item)
            {
                this.grdAirWayBillDetails.CommitEdit();
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    AddDetailRow();
                }), DispatcherPriority.Background, null);
            }
        }
>Damn. I was just thinking it could be a timing issue and was within about 10 secs of suggesting that :-}
>
>
>>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
Previous
Reply
Map
View

Click here to load this message in the networking platform