gpt4 book ai didi

c# - 在DataGrid中选择多个单元格并输出到Excel工作表

转载 作者:行者123 更新时间:2023-11-30 17:18:42 26 4
gpt4 key购买 nike

我有一个包含数百行和两列数据的 DataGrid。我需要以编程方式选择 DataGrid 中的特定行和特定单元格,并将它们输出到 Excel 工作表上。在做了一些研究之后,似乎获得单元格值的唯一方法是编写一个辅助函数,并且每次获得一个单元格值时辅助函数都会运行。

是否有更简单/更快捷的方法来获取 DataGrid 中特定单元格的值?或者,当每个单元格值转到 DataGrid 并将数组输出到 Excel 而不是 DataGrid 时,我是否应该同时将每个单元格值存储到一个数组中?

最佳答案

假设 DataGrid 显示员工信息,我们为每个员工存储他的 IdNameAddress。让我们为每个员工属性添加一个额外的 bool 属性(例如 IdIsIdSelectedNameIsNameSelected)这个 bool 值属性将绑定(bind)到 DataGrid.IsSelected 属性,因此,它表示单元格是否被选中,您也可以通过将其设置为 来使用它以编程方式选择显示指定属性的单元格>正确。见以下代码:

/// <summary>
/// A class that represents an employee. Notice that with each property
/// (e.g. Id) there is a Boolean property that has a similar name (e.g. IsIdSelected)
/// this Boolean property will be bound to the relevant DataGridCell.IsSelected
/// property to indicate whether the cell representing this property were selected
/// or not. In other words if you want to know the selected properties at any moment
/// you just need to iterate through the employees collection, and examine each
/// Boolean property for each property :D
/// </summary>
public class Employee
{
public int? Id { get; set; }
public bool IsIdSelected { get; set; }

public string Name { get; set; }
public bool IsNameSelected { get; set; }

public string Address { get; set; }
public bool IsAddressSelected { get; set; }
}

背后的代码:

using System.Collections.ObjectModel;
using System.Windows;

namespace CellSelection
{
public partial class MainWindow : Window
{
/// <summary>
/// The DataGrid will be bound to this collection
/// </summary>
private ObservableCollection<Employee> _collection;

public MainWindow()
{
InitializeComponent();

// Initialize the employees collection with some test data
_collection =
new ObservableCollection<Employee>
{
new Employee {Id = 1, Name = "Mohammed A. Fadil", Address = "..."},
new Employee {Id = 485, Name = "Khalid Zein", Address = "..."},
new Employee {Id = 64, Name = "Ahmed Mubarak", Address = "..."},
new Employee {Id = 364, Name = "Ali Ebraheim", Address = "..."},
};

DataContext = _collection;
}

private void OnExportButtonClick(object sender, RoutedEventArgs e)
{
// Now, concatinate all the selected cells
var str = string.Empty;
foreach (var emp in _collection)
{
if (emp.IsIdSelected)
str += string.Format("{0}, ", emp.Id);

if (emp.IsNameSelected)
str += string.Format("{0}, ", emp.Name);

if (emp.IsAddressSelected)
str += string.Format("{0}", emp.Address);

str += "\n";
}
// Instead of displaying this message you could export these cells to Excel
// in the format you need.
MessageBox.Show(str);
}
}
}

XAML 代码:

<Window x:Class="CellSelection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="275*" />
<RowDefinition Height="36*" />
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False"
SelectionMode="Extended" SelectionUnit="CellOrRowHeader">
<DataGrid.Columns>

<DataGridTextColumn Header="Id" Binding="{Binding Id}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsSelected">
<Setter.Value>
<Binding Path="IsIdSelected" Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>

<DataGridTextColumn Header="Name" Binding="{Binding Name}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsSelected">
<Setter.Value>
<Binding Path="IsNameSelected" Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>

<DataGridTextColumn Header="Address" Binding="{Binding Address}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsSelected">
<Setter.Value>
<Binding Path="IsAddressSelected" Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Export Selection" Grid.Row="1" HorizontalAlignment="Right"
Click="OnExportButtonClick" Margin="5"/>
</Grid>
</Window>

请注意,对于每个 DataGrid 列,您必须添加一个 CellStyle,将每个列指定的 bool 属性绑定(bind)到它的 DataGridCell.IsSelected 属性,将此绑定(bind)模式设置为 Mode="TwoWay" 以支持以编程方式选择单元格(在这种情况下,您必须在 Employee 类)。

使用此解决方案,您不再需要辅助函数,也不再需要访问 DataGrid 实际单元格,只需遍历您的集合、确定所选属性并根据需要处理它们。

关于c# - 在DataGrid中选择多个单元格并输出到Excel工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5550033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com