2012-11-22 73 views
0

我正在將學生加載到我的datagrid中,然後想要將它們分配給一個類。我真的不在乎如何,但我不能得到任何方法的工作。從dataGrid中的選定行中獲取值

我嘗試了一個上下文菜單,但沒有設法得到事件(從來沒有使用它之前) 然後我在每一行添加了一個按鈕,但後來我無法弄清楚如何獲得行中按鈕是。 我最後的嘗試是獲取selectedItem並像使用組合框一樣使用ToString(),但這也不起作用。

檢查了這一點:

private void dataGrid4_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     DataGrid dg = new DataGrid(); 
     dg = (sender as DataGrid); 
     MessageBox.Show(dataGrid4.Items.GetItemAt(1).ToString()); 
     MessageBox.Show(dataGrid4.CurrentItem.ToString() + "\n" + dataGrid4.CurrentCell.Column.ToString() + "\n" + dataGrid4.CurrentCell.Item.ToString() + "\n" + dataGrid4.SelectedItem.ToString() + "\n" + dataGrid4.SelectedValue.ToString() + "\n" + dataGrid4.SelectedIndex.ToString()); 
     MessageBox.Show(dg.CurrentItem.ToString() + "\n" + dg.CurrentCell.Column.ToString() + "\n" + dg.CurrentCell.Item.ToString() + "\n" + dg.SelectedItem.ToString() + "\n" + dg.SelectedValue.ToString() + "\n" + dg.SelectedIndex.ToString()); 
    } 

請幫我,我快要崩潰了..):

+1

我覺得你的問題已經在當前博客的前一個問題中回答了。請查看鏈接找到您的答案: http://stackoverflow.com/questions/5121186/datagrid-get-selected-rows-column-values –

+0

謝謝,我會去與kmatyaszeks回答:) – oybcs

回答

4

您可以得到所選的項目與電網這樣的:

<DataGrid Name="dgStudents" 
        AutoGenerateColumns="False" 
        SelectionChanged="dgStudents_SelectionChanged"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="ID" Binding="{Binding ID}" /> 
       <DataGridTextColumn Header="Name" Binding="{Binding Name}" /> 
       <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" /> 
      </DataGrid.Columns> 
     </DataGrid> 

事件處理程序:

private void dgStudents_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      if (e.AddedItems != null && e.AddedItems.Count != 0) 
      { 
       foreach (Student item in e.AddedItems) 
       { 
        Console.WriteLine(item.ID); 
       } 
      } 
     } 

簡單的學生類:

class Student 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string Surname { get; set; } 
    } 

如果你想使用上下文菜單試試這個:

<DataGrid Name="dgStudents" 
        AutoGenerateColumns="False"> 
      <DataGrid.ContextMenu> 
       <ContextMenu> 
        <MenuItem Header="ClassA" Click="ClassA_Click" /> 
        <MenuItem Header="ClassB" Click="ClassB_Click" /> 
       </ContextMenu> 
      </DataGrid.ContextMenu> 

      <DataGrid.Columns> 
       <DataGridTextColumn Header="ID" Binding="{Binding ID}" /> 
       <DataGridTextColumn Header="Name" Binding="{Binding Name}" /> 
       <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" /> 
       <DataGridTextColumn Header="Class" Binding="{Binding ClassName}" /> 
      </DataGrid.Columns> 
     </DataGrid> 

添加兩個事件處理MenuItem在後臺代碼:

private void ClassA_Click(object sender, RoutedEventArgs e) 
     { 
      if (dgStudents.SelectedItems != null && dgStudents.SelectedItems.Count != 0) 
      { 
       foreach (Student item in dgStudents.SelectedItems) 
       { 
        item.ClassName = "ClassA"; 
       } 
       dgStudents.Items.Refresh(); 
      } 
     } 

     private void ClassB_Click(object sender, RoutedEventArgs e) 
     { 
      if (dgStudents.SelectedItems != null && dgStudents.SelectedItems.Count != 0) 
      { 
       foreach (Student item in dgStudents.SelectedItems) 
       { 
        item.ClassName = "ClassB"; 
       } 
       dgStudents.Items.Refresh(); 
      } 
     } 

更改學生類這個:

class Student 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string Surname { get; set; } 
     public string ClassName { get; set; } 
    } 
+0

還沒試過,但它看起來像工作代碼:P謝謝! – oybcs

+0

它的工作:)不客氣:) – kmatyaszek