WPF datagrid 动态增加列
DataGrid动态增加列

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="Add Column" Click="AddColumn_Click" Margin="5"/>
<Button Content="Add Data" Click="AddData_Click" Margin="5"/>
</StackPanel>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Grid.Row="1"/>
</Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data; namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<ExpandoObject> items = new ObservableCollection<ExpandoObject>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
dynamic item = new ExpandoObject();
item.A = "Property A value - " + i.ToString();
item.B = "Property B value - " + i.ToString();
items.Add(item);
} dataGrid.Columns.Add(new DataGridTextColumn() { Header = "A", Binding = new Binding("A") });
dataGrid.Columns.Add(new DataGridTextColumn() { Header = "B", Binding = new Binding("B") });
dataGrid.ItemsSource = items;
} private void AddData_Click(object sender, RoutedEventArgs e)
{
dynamic item = new ExpandoObject();
item.A = "New Item - A";
item.B = "New Item - B";
items.Add(item);
} int newColumnIndex = 1;
private void AddColumn_Click(object sender, RoutedEventArgs e)
{
foreach (IDictionary<String, Object> item in items)
{
item.Add("NewColumn" + newColumnIndex, "New Column Value - " + newColumnIndex.ToString());
} dataGrid.Columns.Add(new DataGridTextColumn() { Header = "New Column" + newColumnIndex, Binding = new Binding("NewColumn" + newColumnIndex) }); newColumnIndex++;
}
}
}

https://social.microsoft.com/Forums/zh-CN/69ac1292-9512-4bd3-b691-795f64cb0aa0/wpf-datagrid-?forum=wpfzhchs
WPF datagrid 动态增加列的更多相关文章
- WPF Datagrid 动态生成列 并绑定数据
原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用 可 ...
- Wpf DataGrid动态添加列,行数据(一)
由于最近有这方面的需求,而且刚接触wpf不久,在网上找了很多方法,都不是使用MVVM模式的,因为DataGrid的列不能绑定 这就难受了,我想了个折中的方法,这个是使用了MVVMLight的消息机制, ...
- Wpf DataGrid动态添加列,行数据(二)
这是第二中方法,可直接绑定,我这里只是做出了一种思路,并不是最完美. 这里注意一下,因为我里面引用了MVVMLight,所以可能代码不是复制过去就能用了的. 样式也是,所以复制过去看不是我贴出来的界面 ...
- WPF DataGrid动态生成列的单元格背景色绑定
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.DisplayInde ...
- WPF DataGrid添加编号列
WPF DataGrid添加编号列? 第一步:<DataGridTemplateColumn Header="编号" Width="50" MinWidt ...
- EasyUI datagrid动态生成列
任务描述:根据用户选择时间段,生成列数据,如图
- WPF DataGrid支持的列类型
WPF DataGrid支持下面几种列类型: DataGridTextColumn DataGridCheckBoxColumn DataGridComboBoxColumn DataGridHype ...
- WPF + RDLC + 动态生成列 + 表头合并
如下,评论超过20条,马上发代码*(੭*ˊᵕˋ)੭*ଘ,效果如下: 代码逻辑简单. WPF使用RDLC需要使用如下DLL 新建WPF 窗体,黏贴下大概如下 <Window xmlns:rv=&q ...
- WPF DataGrid绑定及列居中
基本的数据绑定 把集合的字段(属性)绑定在DataGrid的Binding属性就能将数据绑定列表 public class CashItem { public int Value { get; set ...
随机推荐
- nyoj130 相同的雪花
相同的雪花 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You may have heard that no two snowflakes are alike. ...
- Android 开发技巧 - Android 6.0 以上权限大坑和权限检查基类封装
简单介绍 关于运行时权限的说法,早在Google发布android 6.0的时候,大家也听得蛮多的.从用户的角度来讲,用户是受益方,更好的保护用户的意思,而对于开发者来说,无疑增加了工作量. 对于6. ...
- 【leetcode】Remove Duplicates from Sorted List (easy)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- iOS-WKWebView携带cookie发送http请求,cookie失效
发送请求代码: NSString *testUrl = @"http://10.22.122.7:8081/test2_action/view_index"; NSURL *url ...
- Java代码简化神器-Lombok
一.背景 前段时间在开源社区中发现了一个比较牛逼的简化Java代码的神器-Lombok,接着自己写了demo进行测试和练习,感觉真的很不错,特此分享给需要的小伙伴们~ 二.开发之前的准备 1.lomb ...
- android 开发赚钱
原 android 开发赚钱 谁带我去看看世界 发布时间: 2015/06/09 12:05 阅读: 1589 收藏: 37 点赞: 2 评论: 5 开发android也有一年左右了,利用业余时间陆续 ...
- 在python3.5下安装scrapy包
此前scrapy只支持python2.x 但是最新的1.1.0rc1已结开始支持py3了 如果电脑上安装了scrapy的依赖包,诸如lxml.OpenSSL 1.你直接下载Scrapy-1.1.0rc ...
- java web开发环境配置
一:安装jdk http://www.java.com/zh_TW/ 二:下载tomcat,这里是zip版.http://tomcat.apache.org/download-70.cgi 三:在 ...
- ASP.NET MVC中Controller返回值类型ActionResult
1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...
- HTTP/1.1 中 If-Modified-Since 和 If-Unmodified-Since 区别简记
接触HTTP/1.1的时日还不多, 有时候看着这两个参数老是有点混淆, 今天终于理清了, 现记录下. 初学网络, 若有理解不对, 还请拍砖. If-Modified-Since: 从字面上看, ...