Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

HTML
<DataGrid Name="dgHome" Height="299" Width="400" Canvas.Top="-18" SelectionChanged="dgHome_SelectionChanged" SelectionMode="Single" SelectionUnit="FullRow" ></DataGrid>


C#
 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
Updated 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)



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