Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding a new row to a datagrid
Message
De
08/04/2013 07:29:01
 
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01570195
Message ID:
01570373
Vues:
31
>Hi,
>
>I'm using datagridview in vb.
>
>What I did notice is that " CanUserAddRows="False" have you thought of setting this to true?
>
>Again in the DGV of VB the column has tabStop that you can set to false, in which case the column is skipped.
>
>Hope (doubt) this helps,
>
>Marc
>
>

Thanks for the idea Marc,

I had tried setting that CanUserAddRows="True" but that has its own weird behaviour. A new blank row gets added, but the user can't tab into it and they have to use the mouse to click twice in order to get the combobox to display.



>
>>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
Répondre
Fil
Voir

Click here to load this message in the networking platform