如何在C#WPF中使用DataGrid的代码在行标头中添加文本
我有一个带有数组的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在寻找的是加载行事件和
What I was looking for was answerd with Loading row event and enter link description here