DataGrid中其他列和行 [英] DataGrid additional column and row
问题描述
<DataGrid Name="grid"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsReadOnly="False"
HeadersVisibility="None"
AutoGenerateColumns="True"
AutoGeneratingColumn="c_dataGrid_AutoGeneratingColumn">
public static DataView GetBindable2DArray<T>(this T[,] array)
{
DataTable dataTable = new DataTable();
for (int i = 0; i < array.GetLength(1); i++)
{
dataTable.Columns.Add(i.ToString(), typeof(Ref<T>));
}
for (int i = 0; i < array.GetLength(0); i++)
{
DataRow dataRow = dataTable.NewRow();
dataTable.Rows.Add(dataRow);
}
DataView dataView = new DataView(dataTable);
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
int a = i;
int b = j;
Ref<T> refT = new Ref<T>(() => array[a, b], z => { array[a, b] = z; });
dataView[i][j] = refT;
}
}
return dataView;
}
public class Ref<T>
{
private readonly Func<T> getter;
private readonly Action<T> setter;
public Ref(Func<T> getter, Action<T> setter)
{
this.getter = getter;
this.setter = setter;
}
public T Value { get { return getter(); } set { setter(value); } }
}
这就是我的code。而我得到这个数据网格:
Thats my code. And i got this DataGrid:
不过,这是额外的行和列。如何只显示我的5×5格不空行?
But Here is extra row and column. How to show only my grid 5x5 without empty row?
推荐答案
我建议你检查GetLength进行的返回值(0)和对GetLength(1),以确保他们做你希望他们。另外,我一直有一个附加行(以动态创建一个新行),当我用数据网格。
I suggest you to check the return values of GetLength(0) and GetLength(1) to make sure that they do what you expect them to. In addition, I always had one additional row (to create a new row on the fly) when I used Data Grids.
更新: 当您设置的IsReadOnly为True(就像我说的,它是由在飞行编辑)和其他列的其他行消失,是不是一个真正的列,这是行头的空间,如右图所示。设置HeadersVisibility当它消失为全部或行。
Update: The additional row disappears when you set IsReadOnly to "True" (like I said, it is made for on the fly edits) and the additional column is not a real column, it is the space of the row header, shown on the right. It disappears when setting HeadersVisibility to "All" or "Row".
这篇关于DataGrid中其他列和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!