方式一:通过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;
}
}
}