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 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 BillWoodruff 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 17 Sep 2012
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