Click here to Skip to main content
15,886,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
C#
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
Updated 12-May-13 8:12am
v2

1 solution

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:
C#
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.
 
Share this answer
 
Comments
suhas kalambe 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900