How to Put New Row at Top of DataGrid

RogerSchlueter-7899 1,426 Reputation points
2025-04-02T00:00:04.76+00:00

I have a DataGrid with a DataTable as its Source. I would like the new row to be at the top of the grid. All of the pages I've looked at for NewItemPlaceholderPosition show it to be a property of an IEditableCollectionView. Is there a way to put the new row at the top while continuing to use a DataTable?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,853 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 121.6K Reputation points
    2025-04-02T09:32:35.02+00:00

    Try to initialize the DataGrid in this manner:

    DataTable dt = ... data table ...
    dataGrid.ItemsSource = dt.DefaultView;
    var i = (IEditableCollectionView)dataGrid.Items;
    i.NewItemPlaceholderPosition =  NewItemPlaceholderPosition.AtBeginning;
    

    where dataGrid is the name of your control.


1 additional answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 5,255 Reputation points Microsoft External Staff
    2025-04-02T06:13:30.1533333+00:00

    Hi, @RogerSchlueter-7899. Welcome to Microsoft Q&A. 

    You could insert data to the top of DataGrid using DataTable.Rows.InsertAt(newRow,0);

    Reference sample code

    MainWindow.xaml

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <DataGrid x:Name="MyDataGrid" RowEditEnding="MyDataGrid_RowEditEnding">
                
            </DataGrid>
            <Button Grid.Row="1" Width="200" Content="Add NewRow"  Height="50" Click="Button_Click"></Button>
        </Grid>
    

    MainWindow.xaml.cs

        public partial class MainWindow : Window
        {
            DataTable dt = new DataTable();
            public MainWindow()
            {
                InitializeComponent();
    
                //Defining initial data
                dt.Columns.Add("Column1");
                dt.Columns.Add("Column2");
    
                dt.Rows.Add("Row1Column1", "Row1Column2");
                dt.Rows.Add("Row2Column1", "Row2Column2");
    
                //Binding
                MyDataGrid.ItemsSource = dt.DefaultView;
                var i = (IEditableCollectionView)MyDataGrid.Items;
                i.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
    
            }
    
            //Adding New Data
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                DataRow newRow = dt.NewRow();
                newRow["Column1"] = "Row3Column1";
                newRow["Column2"] = "Row4Column1";
                dt.Rows.InsertAt(newRow,0);
            }
    
            private void MyDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
            {
                var dataGrid = sender as DataGrid;
                var collectionView = (IEditableCollectionView)dataGrid.Items;
    
                if (collectionView.IsAddingNew)
                {
                    // Get the DataRowView of the new row
                    var newItem = collectionView.CurrentAddItem as DataRowView;
    
                    if (newItem != null)
                    {
                        // Complete the adding operation
                        collectionView.CommitNew();
    
                        // Get the bound DataTable
                        var dataTable = ((DataView)dataGrid.ItemsSource).Table;
    
                        if (dataTable != null)
                        {
                            // Insert a new row at the top of the DataTable
                            var newRow = newItem.Row;
                            dataTable.Rows.Remove(newRow); // Remove the current line first
                            dataTable.Rows.InsertAt(newRow, 0); // Insert at top
                        }
                    }
                }
            }
    
        }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.