C#WPF datagrid如何添加不同的行 [英] C# WPF datagrid how to add a different line
问题描述
我有一个通过类似代码手动创建的数据网格
I have a datagrid which is MANUALLY created through a similar code
for (int row= 0; row< totRows; row++)
{
dynamic newRow = new ExpandoObject();
for (int col = 0; col < numTotCol; col++)
{
string strColumnHeader = columnLabels[col].Replace(' ', '_');
((IDictionary<String, Object>)newRow)[strColumnHeader] = ...
}
dtgResults.Items.Add(newRow);
}
我现在需要的是在MS Excel中被描述为mergin的东西列。
简而言之,我需要在必要时添加注释行。该行必须更大,但不能更改其他列。那就是它必须容纳更多的列。
我尝试过:
我试过的是使用d DataGridCell_Load事件。
我想到类似的东西:
what I need now is something that in MS Excel is described as mergin columns.
In short I need to add a "comment line" when necessary. That line has to be way larger but has not to change the other columns. That is it has to accomodate on more columns.
What I have tried:
What I have tried is to use the d DataGridCell_Load event.
I thought about something like that:
if (row == 3 && column == 0)
{
cell.Width = 300;
cell.Content = "AAAAA A A AA A A A A A AQ A A AA A A A A A A";
}
但这并没有改变任何东西:有问题的行有第一行总是宽度相同。
提前谢谢。
but that didn't change anything: the line in question has the first row which is always the same width.
Thank you in advance.
推荐答案
不要创建手动使用datagrid,但使用ObservableCollection
。
通过这种方式,您可以轻松操作代码中的值,更改可以双向工作。
以下是ListView的示例:
Do not create the datagrid manually but useObservableCollection
.
This way you can easily manipulate values in code behind, changes work both ways.
Here is an example for a ListView:
private ObservableCollection<MyListViewItem> myListViewItems = new ObservableCollection<MyListViewItem>();
ListView1.ItemsSource = this.myListViewItems;
public class MyListViewItem
{
public MyListViewItem()
{
}
public MyListViewItem(string name, string value, string group, string tag, SolidColorBrush color)
{
this.Name = name;
this.Value = value;
this.Group = group;
this.Tag = tag;
this.StatusColor = color;
}
public string Name { get; set; }
public string Value { get; set; }
public string Group { get; set; }
public string Tag { get; set; }
public SolidColorBrush StatusColor { get; set; }
}
这篇关于C#WPF datagrid如何添加不同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!