更新时间:2023-10-03 20:33:40
你有没有尝试像
dtgResults.Columns.Add(new DataGridTextColumn {标题=a.1});
dtgResults.Columns.Add(new DataGridTextColumn {Header =a.2});
编辑:
This is the continuation to
I have seen this post:
the datagrid is simply this:
<DataGrid Name="dtgResults" Background="Transparent" AutoGenerateColumns="False"/>
in which everything seems to work fine but when I put it in my solution it doesn't compile:
Can anyone explain me why?
---EDIT---
I realize now that I have misunderstood what's in the link above. In short I have a datagrid binded to an observable collection. I have to add two more columns. How can that be done?
---EDIT2---- for CBreeze
dtgResults.ItemsSource = obcmMyDim;<--------previous data here
DataGridTextColumn textColumn1 = new DataGridTextColumn();
textColumn1.Header = "AAA1";
textColumn1.Binding = new Binding("AAA1");
DataGridTextColumn textColumn2 = new DataGridTextColumn();
textColumn2.Header = "AAA2";
textColumn2.Binding = new Binding("AAA2");
Application.Current.Dispatcher.BeginInvoke(new ThreadStart(() => dtgResults.Columns.Add(textColumn1)));
Application.Current.Dispatcher.BeginInvoke(new ThreadStart(() => dtgResults.Columns.Add(textColumn2)));
dtgResults.Items.Add(new { AAA1 = "Col1Row1", AAA2 = "Col2Row1"});
dtgResults.Items.Add(new { AAA1 = "Col1Row2", AAA2 = "Col2Row2" });
---EDIT 3--- for JH So in short I have that observable collection which binded to the datagrid make the following output:
then I add the columns with your method:
var names = obcmMyDim.First().obcItemsName; // All entries must have the same list of obcItemsName and in the same order
for (int i = 0; i < names.Count; i++)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = names[i];
var b = new Binding();
string str = string.Format("obcmMyDim.obcItemsMeasured[{0}]", i);
b.Path = new PropertyPath(str);
b.Mode = BindingMode.TwoWay;
c.Binding = b;
dtgResults.Columns.Add(c);
}
as for the binded array
and the bind str is "obcmMyDim.obcItemsMeasured[0]" ...1....n
but what I get is that the columns are there but they are empty
Have you tried something like;
dtgResults.Columns.Add(new DataGridTextColumn { Header = "a.1"});
dtgResults.Columns.Add(new DataGridTextColumn { Header = "a.2"});
EDIT:
for (int i = 1; i <= 10; i++)
{
dtgResults.Items.Add(new { a.1 = "Test" + i, a.2 = "Test" + i});
}