use the below code where you can add a class name to the datagrid as d1 nad use below properties
.d1 .mx-name-head-row{
position: sticky;
top: 0px;
background-color: #f7f3ff !important;
}
it will work.
Download marketplace module Atlas3Extras and have a look at https://service.mendixcloud.com/p/demodatagridextras for an example of a sticky header.
You can either copy the solution, but the easier option is importing the module into your app and adding class 'sticky-header' to your datagrid.
Hope it helps.