none
WPF Datagrid如何取出某一列的所有值? RRS feed

  • 问题

  •  <DataGrid x:Name="dgTI" DockPanel.Dock="Top" AutoGenerateColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="序号" Width="80"  CanUserSort="False" CanUserResize="False"/>
                                    <DataGridTextColumn Header="TI" Width="100"  CanUserSort="False" CanUserResize="False"/>
                                    <DataGridTextColumn Header="符号" Width="100" CanUserSort="False" CanUserResize="False"/>
                                </DataGrid.Columns>
    </DataGrid>

    1.如何在后台代码中向dgTI添加数据?

    2.如何提取dgTI中第二列的所有数据?只能用for循环么?

    2017年9月29日 7:38

全部回复

  • Hi Shannon Zhou,

    建议你对DataGridTextColumn上加上绑定,这样做更加方便

    1.如何在后台代码中向dgTI添加数据?

    直接对ItemSource赋值即可。

    2.如何提取dgTI中第二列的所有数据?只能用for循环么?

    暂时没有更好的方法。

    我的代码:

    XAML:

            <DataGrid x:Name="GridView" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}" CanUserSort="False" CanUserResize="False"/>
                    <DataGridTextColumn Header="ID" Width="100" Binding="{Binding ID}" CanUserSort="False" CanUserResize="False"/>
                    <DataGridTextColumn Header="Date" Width="100" Binding="{Binding Date}" CanUserSort="False" CanUserResize="False"/>
                </DataGrid.Columns>
            </DataGrid>

    cs

            private void OnLoaded(object sender, RoutedEventArgs e)
            {
                var items = new List<Item>();
                items.Add(new Item() { Name = "Lee", ID = 0, Date = DateTime.Today });
                items.Add(new Item() { Name = "Glen", ID = 1, Date = DateTime.Today.AddDays(1) });
                items.Add(new Item() { Name = "Maxim", ID = 2, Date = DateTime.Today.AddDays(-1) });
                items.Add(new Item() { Name = "Yana", ID = 3, Date = DateTime.Today.AddDays(2) });
                GridView.ItemsSource = items;
            }

        public class Item
        {
            public string Name { get; set; }
            public int ID { get; set; }
            public DateTime Date { get; set; }
        }

    Best wishes,
    Lee

    2017年9月29日 9:29
  • Hi,

    >>1.如何在后台代码中向dgTI添加数据?

    你应该绑定一个集合到DataGrid的ItemSource属性上,Lee的方案很好,但是你最好使用ObsercableCollection<T>,当ObservableCollection<T>添加一行时,会自动通知绑定该ObservableCollection<T>的控件并做相应修改。

    >>2.如何提取dgTI中第二列的所有数据?只能用for循环么?

    http://blog.csdn.net/honantic/article/details/49509641


     public  DataGridRow GetRow( DataGrid dataGrid, int rowIndex)
            {
                DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
                if (rowContainer == null)
                {
                    dataGrid.UpdateLayout();
                    dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
                    rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
                }
                return rowContainer;
            }
    

    获取第二行对应的对象:

     Student stu = (Student)GetRow(dataGrid, 2)?.DataContext;

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年10月2日 8:20
    版主