如何获取datagridview中的所有行?

时间:2019-06-10 20:39:00

标签: c# wpf linq

我将datagridview与名为DGItem的类绑定,并通过linq查询获取了datagrid的值,我想获取所有这些值

试图从DGItem中获取它,但只有我得到的最后一个值

CollegeContext cd = new CollegeContext();          
Teacher teaherCB = _TeachersComboBox.SelectedItem as Teacher;         

var query = (from sc in cd.SubjectTeachers
             join s in cd.Subjects on sc.IdSubject equals s.Id
             join t in cd.Teachers on sc.IdTeacher equals t.Id
             join wh in cd.WorkHours on t.IdWorkHours equals wh.Id
             join b in cd.Branches on sc.IdBranch equals b.Id
             where sc.IdTeacher== teaherCB.Id

             select new DGItem
             {
                 Name = t.Name,
                 Rank = wh.Rank,
                 HoursOfquerm = wh.HoursOfQuorum,
                 Namesub = s.Name,
                 Academic = s.Academic,
                 Virtual = s.Virtual, 
                 Exprement = s.Exprement,
                 TotalHour = s.TotalHours

             } ).ToList();

IstmaraB.ItemsSource = query;

public class DGItem            
    {            
        public String Name { get; set; }          
        public String Rank { get; set; }           
        public int? HoursOfquerm { get; set; }          
        public String Namesub { get; set; }          
        public int? Academic { get; set; }         
        public int? Virtual { get; set; }           
        public int? Exprement { get; set; }          
        public int? TotalHour { get; set; }        

    }  

我的wpf是:

 <Button Content="select all" HorizontalAlignment="Left" Margin="11,382,0,0" VerticalAlignment="Top" Width="94" Click="Button_Click"/>

 <DataGrid x:Name="IstmaraB" HorizontalAlignment="Left" AutoGenerateColumns="False" IsReadOnly="True"  Height="327" EnableRowVirtualization="True" ItemsSource="{Binding }" FlowDirection="RightToLeft" Margin="110,147,0,0" VerticalAlignment="Top" Width="652" RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridTextColumn   Binding="{Binding Name}"  Header="الاسم" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding Rank}"  Header="الدرجة" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding HoursOfquerm}"  Header="النصب القانوني" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding Namesub}"  Header="المواد التي يقوم بتدريسها" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding Academic}"  Header="نظري" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding Virtual}"  Header="عملي" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding Exprement}"  Header="تمارين" Width="auto"/>
            <DataGridTextColumn   Binding="{Binding TotalHour}"  Header="مجموع" Width="auto"/>
        </DataGrid.Columns>
    </DataGrid>

我试图这样做:

 private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        IstmaraB.SelectAll();
        if (IstmaraB.SelectedItems.Count > 0)
        {
            for (int i = 0; i < IstmaraB.SelectedItems.Count; i++)
            {
                System.Data.DataRowView selectedFile = (System.Data.DataRowView)IstmaraB.SelectedItems[i];
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

我很确定您的错误是此行

  

System.Data.DataRowView selectedFile =   (System.Data.DataRowView)IstmaraB.SelectedItems [i];

在我阅读它的过程中,它所发生的一切恰好是最后返回。我也不确定您所说的“获取”是什么意思。您是说要放入数组还是列表?如果就是这样,您可以使用上面的代码在数据网格中简单地在数组中填充值,

  

MyArray [i] = IstmaraB.SelectedItems [i];

如果那不是您的要求,则可能需要澄清。