Click here to Skip to main content
11,581,982 members (74,948 online)
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 1:50am
Edited 17-Sep-12 1: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 330
1 Sergey Alexandrovich Kryukov 165
2 virusstorm 109
3 Frankie-C 105
4 F-ES Sitecore 85
0 OriginalGriff 1,273
1 Sergey Alexandrovich Kryukov 900
2 Abhinav S 617
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web03 | 2.8.150603.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