DataGrid 附加列和行

发布于 2024-10-17 08:58:07 字数 1807 浏览 5 评论 0原文

<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:

enter image description here

But Here is extra row and column. How to show only my grid 5x5 without empty row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2024-10-24 08:58:07

我建议您检查 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文