Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
Hi ,
Am new to wpf. I am using wpf DataGrid in my project.My requirement is while loading datagrid first row should be selected after selecting the first row need to one event should be occur which is dgHome_selectionChenged
 
 
            <DataGrid Name="dgHome" Height="299" Width="400" Canvas.Top="-18" SelectionChanged="dgHome_SelectionChanged" SelectionMode="Single" SelectionUnit="FullRow" ></DataGrid>
        
 
 private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
                        bindGrid();
            
          
        }
 
  private void bindGrid()
        {
            try
            {
                cTappal objTappal = new cTappal();                
                DataSet ds= new DataSet ();
                ds=objTappal .GetTappalIssueWise();                
                dgHome.ItemsSource = ds.Tables[0].DefaultView;             
                dgHome.SelectedItem = dgHome.Items[0];
<big>// Heare i need to some code to generate that event which is dgHome_SelectionChanged</big> 
                        
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
 
private void dgHome_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
   try
    {
      DataGrid dataGrid = sender as DataGrid;
 
      if (e.AddedItems != null && e.AddedItems.Count > 0)
       {
       // find row for the first selected item
                    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
 
                 if (row != null)
                    {
                        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
 
                        // find grid cell object for the cell with index 0
                        DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
 
             if (cell != null)
            {
                                                   
             int docID =    Convert.ToInt32(((TextBlock)cell.Content).Text);
             dgTappal.ItemsSource = GetData(docID).Tables[0].DefaultView;
 
             }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
        }
 

Please help me.
Posted 17-Sep-12 2:50am
Edited 17-Sep-12 2:53am
v2

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

  Print Answers RSS
0 Maciej Los 580
1 OriginalGriff 420
2 Sascha Lefévre 289
3 Abhinav S 220
4 CHill60 200
0 Sergey Alexandrovich Kryukov 9,773
1 OriginalGriff 9,290
2 Peter Leow 5,137
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,036


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2015
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