suhas kalambe

suhas kalambe

  • NA
  • 1
  • 1.6k

WPF C# datagrid fix view row (fix scroll)

May 19 2013 2:27 AM

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.