WPF DataGrid 动态增加列

方式一:通过DataGrid 数据源即DataTable,在DataTable里面动态增加了列之后,重新构造每一行数据,设置DataGrid.ItemsSource = null; 然后再重新设置ItemsSource到DataTable

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        int newColumnIndex = 1;
        private void AddColum_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = (DataGridtest.ItemsSource as DataView).ToTable();
            if (DataGridtest.ItemsSource == null)
            {
                return;
            }
            dt.Columns.Add(new DataColumn("New Column" + newColumnIndex++));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][dt.Columns.Count - 1] = i.ToString() + " - New Column";
            }
            DataGridtest.ItemsSource = null;
            DataGridtest.ItemsSource = dt.DefaultView;
        }

        private void ShowClomun_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();

            for (int i = 0; i < 100; i++)
            {
                dt.Columns.Add(new DataColumn(string.Format("{0}{1}", "Colum", i)));
            }
            DataRow dr;
            for (int i = 0; i < 200; i++)
            {
                dr = dt.NewRow();
                for (int columIndex = 0; columIndex < dt.Columns.Count; columIndex++)
                    dr[columIndex] = i.ToString() + " - " + columIndex.ToString();
                dt.Rows.Add(dr);
            }
            DataGridtest.ItemsSource = dt.DefaultView;
        }
}

方式二:通过ObservableCollection<dynamic>集合,它可以动态的添加属性,这种类型添加好属性之后,重新设置DataGrid的ItemsSource绑定即可

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DynymicDataGrid
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
              List<string> header = new List<string>();
        ObservableCollection<dynamic> items = new ObservableCollection<dynamic>();
        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                header.Add("Colum" + i);
            }
        }
        int newColumnIndex = 1;

        private void AddColum_Click(object sender, RoutedEventArgs e)
        {
            foreach (ExpandoObject item in items)
            {
                dynamic x = item;
                x.NewColumn = "NewColumn";
            }

            DataGridtest.Columns.Add(new DataGridTextColumn() { Header = "NewColumn", Binding = new Binding("NewColumn") });

            newColumnIndex++;
        }

        private void ShowClomun_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                dynamic item = new ExpandoObject();
                foreach (var vheader in header)
                {
                    item.A1 = "A value - " + i.ToString();
                    item.A2 = "A value - " + i.ToString();
                    item.A3 = "A value - " + i.ToString();
                    item.A4 = "A value - " + i.ToString();
                    item.A5 = "A value - " + i.ToString();
                    item.A6 = "A value - " + i.ToString();
                    item.A7 = "A value - " + i.ToString();
                    item.A8 = "A value - " + i.ToString();
                    item.A9 = "A value - " + i.ToString();
                    item.A10 = "A value - " + i.ToString();
                    item.A10 = "A value - " + i.ToString();
                    item.A12 = "A value - " + i.ToString();
                    item.A13 = "A value - " + i.ToString();
                    item.A14 = "A value - " + i.ToString();
                    item.A15 = "A value - " + i.ToString();
                    item.A16 = "A value - " + i.ToString();
                    item.A17 = "A value - " + i.ToString();
                    item.A18 = "A value - " + i.ToString();
                    item.A19 = "A value - " + i.ToString();
                    item.A20 = "A value - " + i.ToString();
                    item.A21 = "A value - " + i.ToString();
                    item.A22 = "A value - " + i.ToString();
                    item.A23 = "A value - " + i.ToString();
                    item.A24 = "A value - " + i.ToString();
                    item.A25 = "A value - " + i.ToString();
                    item.A26 = "A value - " + i.ToString();
                    item.A27 = "A value - " + i.ToString();
                    item.A28 = "A value - " + i.ToString();
                    item.A29 = "A value - " + i.ToString();
                    item.A30 = "A value - " + i.ToString();
                    item.A31 = "A value - " + i.ToString();
                    item.A32 = "A value - " + i.ToString();
                    item.A33 = "A value - " + i.ToString();
                    item.A34 = "A value - " + i.ToString();
                    item.A35 = "A value - " + i.ToString();
                    item.A36 = "A value - " + i.ToString();
                    item.A37 = "A value - " + i.ToString();
                    item.A38 = "A value - " + i.ToString();
                    item.A39 = "A value - " + i.ToString();
                    item.A40 = "A value - " + i.ToString();
                    item.A41 = "A value - " + i.ToString();
                    item.A42 = "A value - " + i.ToString();
                    item.A43 = "A value - " + i.ToString();
                    item.A44 = "A value - " + i.ToString();
                    item.A45 = "A value - " + i.ToString();
                    item.A46 = "A value - " + i.ToString();
                    item.A47 = "l value - " + i.ToString();
                    item.A48 = "A value - " + i.ToString();
                    item.A49 = "A value - " + i.ToString();
                    item.A50 = "A value - " + i.ToString();
                    item.A51 = "A value - " + i.ToString();
                    item.A52 = "A value - " + i.ToString();
                    item.A53 = "A value - " + i.ToString();
                    item.A54 = "A value - " + i.ToString();
                    item.A55 = " A value - " + i.ToString();
                    item.A56 = " A value - " + i.ToString();
                    item.A57 = " A value - " + i.ToString();
                    item.A58 = " A value - " + i.ToString();
                    item.A59 = " A value - " + i.ToString();
                    item.A60 = " A value - " + i.ToString();
                    item.A61 = " A value - " + i.ToString();
                    item.A62 = " A value - " + i.ToString();
                    item.A63 = " A value - " + i.ToString();
                    item.A64 = " A value - " + i.ToString();
                    item.A65 = " A value - " + i.ToString();
                    item.A66 = " A value - " + i.ToString();
                    item.A67 = " A value - " + i.ToString();
                    item.A68 = " A value - " + i.ToString();
                    item.A69 = " A value - " + i.ToString();
                    item.A70 = " A value - " + i.ToString();
                    item.A71 = " A value - " + i.ToString();
                    item.A72 = " A value - " + i.ToString();
                    item.A73 = " A value - " + i.ToString();
                    item.A74 = " A value - " + i.ToString();
                    item.A75 = " A value - " + i.ToString();
                    item.A76 = " A value - " + i.ToString();
                    item.A77 = " A value - " + i.ToString();
                    item.A78 = " A value - " + i.ToString();
                    item.A79 = "A value - " + i.ToString();
                    item.A80 = "A value - " + i.ToString();
                    item.A81 = " A value - " + i.ToString();
                    item.A82 = " A value - " + i.ToString();
                    item.A83 = " A value - " + i.ToString();
                    item.A84 = " A value - " + i.ToString();
                    item.A85 = " A value - " + i.ToString();
                    item.A86 = " A value - " + i.ToString();
                    item.A87 = " A value - " + i.ToString();
                    item.A88 = " A value - " + i.ToString();
                    item.A89 = " A value - " + i.ToString();
                    item.A90 = " A value - " + i.ToString();
                    item.A91 = " A value - " + i.ToString();
                    item.A92 = " A value - " + i.ToString();
                    item.A93 = " A value - " + i.ToString();
                    item.A94 = " A value - " + i.ToString();
                    item.A95 = " A value - " + i.ToString();
                    item.A96 = " A value - " + i.ToString();
                    item.A97 = " A value - " + i.ToString();
                    item.A98 = " A value - " + i.ToString();
                    item.A99 = "A value - " + i.ToString();
                }
                items.Add(item);
            }
            for (int i = 0; i < 40; i++)
            {
                DataGridtest.Columns.Add(new DataGridTextColumn() { Header = "A" + i, Binding = new Binding("A" + i) });
            }
            DataGridtest.ItemsSource = items;
        }
    }
}
相关推荐
当下就是最好15 小时前
WPF应用程序的生命周期-笔记
wpf
九鼎科技-Leo1 天前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
麻花20132 天前
C#之WPF的C1FlexGrid空间的行加载事件和列事件变更处理动态加载的枚举值
开发语言·c#·wpf
lcintj2 天前
【WPF】Prism学习(九)
学习·wpf·prism
界面开发小八哥2 天前
界面控件DevExpress WPF中文教程:网格视图数据布局的列和卡片字段
wpf·界面控件·devexpress·ui开发·用户界面
△曉風殘月〆2 天前
如何在WPF中嵌入其它程序
wpf
Crazy Struggle2 天前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
shepherd枸杞泡茶2 天前
WPF动画
c#·.net·wpf
lcintj2 天前
【WPF】Prism学习(十)
学习·wpf·prism
wyh要好好学习2 天前
WPF数据加载时添加进度条
ui·wpf