DataGrid get selected rows' column values

前端 未结 8 1292
你的背包
你的背包 2020-11-27 06:11

I\'m trying to get the values of each column of a selected row in a DataGrid. This is what I have:

private void dataGrid1_CellEditEnding(object sender, DataG         


        
相关标签:
8条回答
  • 2020-11-27 06:37

    UPDATED

    To get the selected rows try:

    IList rows = dg.SelectedItems;
    

    You should then be able to get to the column value from a row item.

    OR

    DataRowView row = (DataRowView)dg.SelectedItems[0];
    

    Then:

    row["ColumnName"];
    
    0 讨论(0)
  • 2020-11-27 06:37

    Solution based on Tonys answer:

            DataGrid dg = sender as DataGrid;
            User row = (User)dg.SelectedItems[0];
            Console.WriteLine(row.UserID);
    
    0 讨论(0)
  • 2020-11-27 06:38

    I used a similar way to solve this problem using the animescm sugestion, indeed we can obtain the specific cells values from a group of selected cells using an auxiliar list:

    private void dataGridCase_SelectionChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            foreach (var item in e.AddedCells)
            {
                var col = item.Column as DataGridColumn;
                var fc = col.GetCellContent(item.Item);
                lstTxns.Items.Add((fc as TextBlock).Text);
            }
        }
    
    0 讨论(0)
  • 2020-11-27 06:40

    I did something similar but I use binding to get the selected item :

    <DataGrid Grid.Row="1" AutoGenerateColumns="False" Name="dataGrid"
              IsReadOnly="True" SelectionMode="Single"
              ItemsSource="{Binding ObservableContactList}" 
              SelectedItem="{Binding SelectedContact}">
      <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
        <DataGridTextColumn Binding="{Binding Path=FamilyName}" Header="FamilyName"/>
        <DataGridTextColumn Binding="{Binding Path=Age}" Header="Age"/>
        <DataGridTextColumn Binding="{Binding Path=Relation}" Header="Relation"/>
        <DataGridTextColumn Binding="{Binding Path=Phone.Display}" Header="Phone"/>
        <DataGridTextColumn Binding="{Binding Path=Address.Display}" Header="Addr"/>
        <DataGridTextColumn Binding="{Binding Path=Mail}" Header="E-mail"/>
      </DataGrid.Columns>
    </DataGrid>
    

    So I can access my SelectedContact.Name in my ViewModel.

    0 讨论(0)
  • 2020-11-27 06:42

    After hours of finding ways on how to get the data from the row selected on a WPF DataGrid Control, as I was using MongoDB. I found this post and used Tony's answer. I revised the code to be relevant to my project. Maybe someone can use this to get an idea.

    private void selectionChanged(object sender, SelectionChangedEventArgs e)
        {
            facultyData row = (facultyData)facultyDataGrid.SelectedItem;
            facultyID_Textbox.Text = row.facultyID;
            lastName_TextBox.Text = row.lastName;
            firstName_TextBox.Text = row.firstName;
            middleName_TextBox.Text = row.middleName;
            age_TextBox.Text = row.age.ToString();
        }
    
    }
    
    class facultyData
    {
        public ObjectId _id { get; set; }
        public string facultyID { get; set; }
        public string acadYear { get; set; }
        public string program { get; set; }   
    }
    
    0 讨论(0)
  • 2020-11-27 06:44

    An easy way that works:

    private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        foreach (var item in e.AddedCells)
        {
            var col = item.Column as DataGridColumn;
            var fc = col.GetCellContent(item.Item);
    
            if (fc is CheckBox)
            {
                Debug.WriteLine("Values" + (fc as CheckBox).IsChecked);
            }
            else if(fc is TextBlock)
            {
                Debug.WriteLine("Values" + (fc as TextBlock).Text);
            }
            //// Like this for all available types of cells
        }
    }
    
    0 讨论(0)
提交回复
热议问题