I have an existing application which uses a data grid, the data grid is then tied onto a checkbox that allows users to make specific selections. Let us assume that the datagrid returns/displays 5 columns and when a row is selected, there is an export functionality that exports selected items on the datagrid into a csv file.
My requirement is for the columns to be displayed on the datagrid, however when it comes to the export I only want to export a single column onto the csv file. As the code is quite long, I have extracted only the concerned sections of the code. I feel that its the DataGridServerSet that needs to be altered. I could change the entire query to only return a single column and tis will be exported to csv, but I don't want to do that as the other columns has useful information, however once the user ticks a row for export, only a particular column should be exported onto the csv file.
Section of the code.
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;
}
}