在c#的WPF应用程序中导出数据网格的子集



我有一个使用数据网格的现有应用程序,然后将数据网格绑定到允许用户进行特定选择的复选框上。让我们假设数据网格返回/显示5列,当选择一行时,有一个导出功能,将数据网格上选择的项导出到csv文件中。

我的要求是将列显示在数据网格上,但是当涉及到导出时,我只想将单个列导出到csv文件。由于代码很长,我只提取了代码的相关部分。我觉得DataGridServerSet需要改变。我可以改变整个查询只返回单列,这将被导出到csv,但我不想这样做,因为其他列有有用的信息,但是一旦用户勾选一行导出,只有一个特定的列应该导出到csv文件。

代码段。

private DataView _dgServeryDataSet;
public RelayCommand ExportCommand
{
set => _exportCommand = value;
get
{
_exportCommand = new RelayCommand(ExportCSV);
return _exportCommand;
}
}
public RelayCommand CheckAllBoxesCommand
{
set => _checkAllBoxesCommand = value;
get
{
_checkAllBoxesCommand = new RelayCommand(CheckAll);
return _checkAllBoxesCommand;
}
}
private void ExportCSV(object obj)
{
DataView DataGridNew = new DataView(DataGridServerSet.Table);
DataTable table2 = DataGridNew.toTable(false,"ServerBuildNo");
Dataview BuildNoView = new DataView(table2);
UiUtilities.ExportDynamicDataView(BuildNoView);
}
private void CheckAll(object obj)
{
if (DataGridServerSet != null)
{
foreach (DataRow row in DataGridServerSet?.Table.Rows)
{
row[CheckBoxColumnName] = CheckAllBoxes;
}
}
}

public DataView DataGridServerSet
{
set
{
_dgServeryDataSet = value;
OnPropertyChanged();
}
get => _dgServeryDataSet;
}

private void ApplyFilters(object obj)
{
Mouse.OverrideCursor = Cursors.Wait;
_dataSet.Clear();
_sqlConnection.Open();
try
{
_dataAdapter.SelectCommand = new SqlCommand(
"EXEC sp_get_list ", _sqlConnection);
DataGridServerSet = CreateDynamicDataViewWithCheckBox(_dataAdapter);
ServerCount = DataGridServerSet.Table.Rows.Count.ToString();
CanShowServerCount = "Visible";
_sqlConnection.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
_sqlConnection.Close();
Mouse.OverrideCursor = null;
}
}

我通过实现下面的代码修复了这个问题。

private DataView _dgServeryDataSet;
public RelayCommand ExportCommand
{
set => _exportCommand = value;
get
{
_exportCommand = new RelayCommand(ExportCSV);
return _exportCommand;
}
}
public RelayCommand CheckAllBoxesCommand
{
set => _checkAllBoxesCommand = value;
get
{
_checkAllBoxesCommand = new RelayCommand(CheckAll);
return _checkAllBoxesCommand;
}
}
private void ExportCSV(object obj)
{
DataView DataGridNew = new DataView(DataGridServerSet.Table);
DataTable table2 = DataGridNew.toTable(false,"checked","ServerBuildNo");
Dataview BuildNoView = new DataView(table2);
UiUtilities.ExportDynamicDataView(BuildNoView);
}
private void CheckAll(object obj)
{
if (DataGridServerSet != null)
{
foreach (DataRow row in DataGridServerSet?.Table.Rows)
{
row[CheckBoxColumnName] = CheckAllBoxes;
}
}
}

public DataView DataGridServerSet
{
set
{
_dgServeryDataSet = value;
OnPropertyChanged();
}
get => _dgServeryDataSet;
}

private void ApplyFilters(object obj)
{
Mouse.OverrideCursor = Cursors.Wait;
_dataSet.Clear();
_sqlConnection.Open();
try
{
_dataAdapter.SelectCommand = new SqlCommand(
"EXEC sp_get_list ", _sqlConnection);
DataGridServerSet = CreateDynamicDataViewWithCheckBox(_dataAdapter);
ServerCount = DataGridServerSet.Table.Rows.Count.ToString();
CanShowServerCount = "Visible";
_sqlConnection.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
_sqlConnection.Close();
Mouse.OverrideCursor = null;
}
}

最新更新