0

I added a record in a table of SQLServer with WPF-application and refresh DataGrid shown a new record. For instance, I add user which has name "Peter, last name "Pen" and this record adds at the end of DataGrid. How to move focus on this record and highlight on that? In other words, how to move focus and highlight by name or surname?

ViewModel has such code:

<Window x:Class="Students.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        Title="MainWindow" Height="996" Width="1191" xmlns:my="clr-namespace:Students" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen">    
    <Window.Resources>        
        <my: StudentDataSet x:Key="StudentDataSet" />
        <CollectionViewSource x:Key="StudentViewSource" Source="{Binding Path=S_DEP, Source={StaticResource StudentDataSet}}" />          
    </Window.Resources>

<Grid>
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="615" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource StudentViewSource}}" Margin="21,322,0,0" Name="StudentDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="1046">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="NameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" MinWidth="110" />
                <DataGridTextColumn x:Name="LastNameColumn" Binding="{Binding Path=LastName}" Header="LastName" Width="SizeToHeader" MinWidth="100"/>                
                <DataGridTextColumn x:Name="PhoneColumn" Binding="{Binding Path=PHONE}" Header="Phone Number" Width="SizeToHeader" MinWidth="105" />               
            </DataGrid.Columns>
        </DataGrid>
</Grid>

Model has such code:

UserBoard.StudentDataSet aRCHDOC_1DataSet = ((UserBoard.StudentDataSet)(this.FindResource("StudentDataSet")));            
            // Loading data in the table Student            UserBoard.StudentDataSetTableAdapters.StudentTableAdapter StudentDataSet_StudentTableAdapter = new UserBoard.StudentDataSetTableAdapters.StudentTableAdapter();
            StudentDataSet_StudentTableAdapter.Fill(StudentDataSet.Students);
            System.Windows.Data.CollectionViewSource StudentViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("StudentViewSource")));
            StudentViewSource.View.MoveCurrentToFirst();


            //Highlighting a necessary row
            string position = e.Someth_property;
            for (int i = 0; i < StudentDataGrid.Items.Count; i++)
            {
                //What do I should write here?   
            }

Please, as a kindness to me! Give examples for WPF 2010 as code of Visual C# does not work in WPF 2010.

StepUp
  • 36,391
  • 15
  • 88
  • 148

2 Answers2

1

If you want to set focus on last added row then try this code:

dataGridView.ClearSelection();
int RwIndex= dataGridView.Rows.Count - 1;

dataGridView.Rows[RwIndex].Selected = true;
dataGridView.Rows[RwIndex].Cells[0].Selected = true;
Bhavik Patel
  • 752
  • 1
  • 5
  • 20
  • Thanks, but it does not work in WPF due to the fact that this code for Visual C#. Code Visual C# does not work in WPF. – StepUp Oct 17 '12 at 03:56
  • Yes, But you can use this in WPF for selection. Actually in WPF you have to use ListView to do like this. For more details refer my another answer. – Bhavik Patel Oct 17 '12 at 05:38
  • If you want to use gridview then you may have to use listview Otherwise no need to use, you can directly use datagrid control. – Bhavik Patel Oct 17 '12 at 16:35
1

For WPF you have to add gridview control inside Listview, after that you can easily select and focus particular record in Gridview. Otherwise you must have to use DataGrid Control for this kind of stuff.

For example (Listview) refer this code:

myListView.SelectedItem = myListView.Items[index];
myListView.ScrollIntoView(myListView.Items[index]);
ListViewItem listViewItem = myListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
listViewItem.Focus(); 

For example (DataGrid):

int index = 11;
myDataGrid.SelectedItem = myDataGrid.Items[index];
myDataGrid.ScrollIntoView(myDataGrid.Items[index]);
DataGridRow dgrow =(DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.Items[index]);
dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Bhavik Patel
  • 752
  • 1
  • 5
  • 20
  • For your information you can use second example in your case.Thanks – Bhavik Patel Oct 17 '12 at 05:52
  • 1. Do I have to create ListView on the same form with DataGrid and to bind ListView to the same adapter of DataFrid? 2. Why should I create ListView cause I want to highlight a row in DataGrid? Maybe you have a link with sample? – StepUp Oct 17 '12 at 13:36
  • Both examples are different to use different methods, And I haven't any link regarding List view. I suggest you because i implement it in my professional stuffs But If you want related example then you can see [here](http://social.msdn.microsoft.com/forums/en-US/wpf/thread/98d8423c-9719-4291-94e2-c5bf3d80cd46/) – Bhavik Patel Oct 17 '12 at 16:49
  • Hey StepUp, I found out one post something like this for data grid, You can find out it over [Here](http://stackoverflow.com/questions/1976087/wpf-datagrid-set-selected-row) , I hope it will helpful to you. Regards! – Bhavik Patel Oct 17 '12 at 16:52
  • I have tried this code:). It does not work. It creates a error. – StepUp Oct 18 '12 at 03:47
  • Which option did you try ? And What error are you facing? Please elaborate. – Bhavik Patel Oct 18 '12 at 06:37
  • I tried a code Serge Gubenko. When I am implementig the code, then the code gets error in this row: TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock; What can we use instead TextBlock? – StepUp Oct 18 '12 at 10:10