如何在C#WPF中使用DataGrid的代码在行标头中添加文本

发布于 2025-02-02 23:58:00 字数 1505 浏览 2 评论 0原文

我有一个带有数组的datagrid。 如何使用后面的代码在行标头中添加文本? 到目前为止,我添加了这样的数据。

public static DataView GetBindable2DArray<T>(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;
    }

XAML

<DataGrid x:Name="tableDataGrid" Grid.Row="0" Grid.RowSpan="6" Grid.Column="0" 
              AutoGenerateColumns="True"
              ColumnWidth="*"
              VerticalScrollBarVisibility="Visible"
              RowHeaderWidth="100" SelectionMode="Extended" SelectionUnit="Cell"
               HeadersVisibility="All" CanUserAddRows="False">
      
    </DataGrid>

项目库

            tableDataGrid.ItemsSource = GetBindable2DArray<double>(dataArray);

I have a DataGrid which is populated with an array.
How do I add text in row header using code behind?
So far I've added data like this.

public static DataView GetBindable2DArray<T>(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;
    }

The XAML

<DataGrid x:Name="tableDataGrid" Grid.Row="0" Grid.RowSpan="6" Grid.Column="0" 
              AutoGenerateColumns="True"
              ColumnWidth="*"
              VerticalScrollBarVisibility="Visible"
              RowHeaderWidth="100" SelectionMode="Extended" SelectionUnit="Cell"
               HeadersVisibility="All" CanUserAddRows="False">
      
    </DataGrid>

ItemSource

            tableDataGrid.ItemsSource = GetBindable2DArray<double>(dataArray);

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

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

发布评论

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

评论(1

我一向站在原地 2025-02-09 23:58:00

我正在寻找的是加载行事件

private void tableDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        e.Row.Header = e.Row.GetIndex();
    }

What I was looking for was answerd with Loading row event and enter link description here

private void tableDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        e.Row.Header = e.Row.GetIndex();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文