Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
i am working on WPF datagrid and want to fix view after insert row at top, whenever row insert at top view scroll down by 1 row, i am using following code
    int i = 0;
    DataTable dt = null;
    
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        dt = new DataTable();
        dt.Columns.Add("Col1");
        dt.Columns.Add("Col2");
 
        DispatcherTimer dtimer = new DispatcherTimer();
        dtimer.Tick += new EventHandler(dt_Tick);
        dtimer.Interval = new TimeSpan(0, 0, 1);
        dtimer.Start();
        dataGrid1.DataContext = dt;
 

    }
    void dt_Tick(object sender, EventArgs e)
    {
        DataRow dr;
        dr = dt.NewRow();
        dr["Col1"] = i;
        dr["Col2"] = i; 
        dt.Rows.InsertAt(dr, 0);
        i++;
    }
i tried to use dataGrid1.ScrollIntoView(dataGrid1.SelectedItem); but it still need selected item, and even it scroll down till last row in view
 
I can manage to do the same in DataGridView of windows form in following way
DataGridViewCell cell = dataGridView1.FirstDisplayedCell;
dt.Rows.InsertAt(dr, 0);
dataGridView1.FirstDisplayedCell = cell;
looking for any similar ways to do this in WPF also
 
Thanks.
Posted 12-May-13 6:24am
Edited 12-May-13 9:12am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
so what's preventing you from making the inserted row either the selected item and/or using the created row to call ScrollIntoView?
 
So it should be enough to write:
void dt_Tick(object sender, EventArgs e)
{
   DataRow dr;
   dr = dt.NewRow();
   dr["Col1"] = i;
   dr["Col2"] = i; 
   dt.Rows.InsertAt(dr, 0);
   i++;
 
   // either do a scroll to inserted row
   dataGrid1.ScrollIntoView(dr);
 
   // or select item
   // dataGrid1.SelectedItem = dr;
   
 
   // and then scroll selected item into view
   // dataGrid1.ScrollIntoView(dataGrid1.SelectedItem);
}
 
Hope this helps.
  Permalink  
Comments
suhas kalambe at 19-May-13 1:41am
   
Hi Thomas,
 
Thanks for post,
 
but
dataGrid1.ScrollIntoView(dr);
this works on new inserted row and it'll come into view, this is fine in one scenario
 
I have one more scenario
I want to freeze my view, like if i am having 3 rows, and after insert 1 more at top i still want old 3 rows in view
 

when i do this
dataGrid1.ScrollIntoView(dataGrid1.SelectedItem);
the selected row not going out of view but it scroll down all the ways in view
 
Please try this in winforms with datagridview
DataGridViewCell cell = dataGridView1.FirstDisplayedCell;
dt.Rows.InsertAt(dr, 0);
dataGridView1.FirstDisplayedCell = cell;
 
i want similar case
 
Thanks.
Thomas Duwe at 21-May-13 3:43am
   
You didn't mention this second case in detail, so I couldn't begin to guess as to what you really want.
But regardless, this second case is almost the same: remember the first/last row you want to be visible and
call dataGrid1.ScrollIntoView(itemToScrollTo);
Remember that itemToScrollTo needs to be the DataRow of your DataTable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100