0

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;
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
learner
  • 545
  • 2
  • 9
  • 23
  • How about create a copy of DataGridServerSet in ExportCSV(), which contains only the column that you want to export. See this [post](https://stackoverflow.com/a/1201399/9265852) – mwck46 Aug 08 '21 at 02:13
  • Thanks for the tip, I have made some edits based on the implementation on the example. I get the error **string was not a recognized as a valid boolean** – learner Aug 08 '21 at 18:40
  • I can't reproduce your problem without more code. If I were you, I'll put `DataView BuildNoView = new DataView(DataGridServerSet.ToTable(false,"ServerBuildNo"));` before `ExportDynamicDataView(BuildNoView)` – mwck46 Aug 09 '21 at 01:42
  • Where exactly are you getting the exception...? – mm8 Aug 09 '21 at 18:37
  • I have managed to solve the issue. as the data grid has a checkbox to know which row was selected. I fixed it by adding. new `DataView(DataGridServerSet.ToTable(false,"checked","ServerBuildNo")); before ExportDynamicDataView(BuildNoView)` – learner Aug 11 '21 at 15:55

1 Answers1

1

I fixed the issue by implementing the below.

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;
    }
}
learner
  • 545
  • 2
  • 9
  • 23