【问题标题】:How to remove few unwanted column in WPF 4.5 Datagrid?如何在 WPF 4.5 Datagrid 中删除一些不需要的列?
【发布时间】:2013-01-10 16:39:10
【问题描述】:

我正在使用下面写的 XAML

<Window x:Class="ERP.WinApp.Views.Admin.Patients"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Patients" Height="auto" MinWidth="1024" Width="1024" ShowInTaskbar="False" Icon="/ERP.WinApp;component/Images/patient.png" WindowStartupLocation="CenterScreen">
    <Grid>
<DataGrid ItemsSource="{Binding}" Grid.Row="1" Name="gridPatients" ></DataGrid>
</Grid>
</Window>

以及下面写的代码

namespace ERP.WinApp.Views.Admin
{
    /// <summary>
    /// Interaction logic for Patients.xaml
    /// </summary>
    public partial class Patients : Window
    {
        public Patients()
        {
            InitializeComponent();

            List<Patient> list = new List<Patient>();
            list = // Populate it through some method
            gridPatients.DataContext = list;
        }

    }
}

Patient 类的简单属性很少

public class Patient
    {

        public int Id { get; set; }

        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string LastName { get; set; }

        public string FullName { get{ return this.FirstName+ " " +this.MiddleName+ " " +this.LastName; } }

        public DateTime DOB { get; set; }

        public int Age { get { return DateTime.Today.Year - this.DOB.Year; } }

        public char Gender { get; set; }
    }

当我运行应用程序时,我的数据网格中的所有列都包含数据,而如果我只想有几列,例如跳过 Id 和年龄以及第一、中间、姓氏,那么最好的方法是什么。

我认为这样做对于我想隐藏的每一列都是不好的方式

gridPatients.Columns[0].Visibility = Visibility.Collapsed;

【问题讨论】:

    标签: c# .net wpf xaml datagrid


    【解决方案1】:

    您可以将AutoGenerateColumns 设置为false,并添加所需的列。

    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" Grid.Row="1" Name="gridPatients" >
     <ItemTemplate>
       <DataTemplate>
          <DataGridTextColumn Binding={ Path=FirstName}/>
        ... 
        <DataTemplate>
     </ItemTemplate>
    </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2014-08-22
      • 2019-10-26
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多