DataGrid 附加列和行
<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); } }
}
这就是我的代码。我得到了这个数据网格:
但是这里有额外的行和列。如何只显示我的 5x5 网格而不显示空行?
<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); } }
}
Thats my code. And i got this DataGrid:
But Here is extra row and column. How to show only my grid 5x5 without empty row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您检查 GetLength(0) 和 GetLength(1) 的返回值,以确保它们按照您的预期执行。此外,当我使用数据网格时,我总是多了一行(用于动态创建新行)。
更新:
当您将 IsReadOnly 设置为“True”时,附加行会消失(就像我说的,它是为动态编辑而设计的),并且附加列不是真正的列,它是行标题的空间,如右侧所示。当将 HeadersVisibility 设置为“All”或“Row”时,它会消失。
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.
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".