Example #1
0
void MyFrame::OnOpen(wxCommandEvent & WXUNUSED(event)) {
    wxFileDialog dialog(this, wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString,
                        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (dialog.ShowModal() == wxID_OK) {
        wxString filename(dialog.GetPath());

        wxTextFile text_file(filename);
        if (!text_file.Open(wxConvUTF8)) {
            return;
        }

        grid->EnableEditing(false);
        if (text_file.GetLineCount() > grid->GetNumberRows()) {
            grid->AppendRows(text_file.GetLineCount() - grid->GetNumberRows());

        } else if (text_file.GetLineCount() < grid->GetNumberRows()) {
            grid->DeleteRows(text_file.GetLineCount(), grid->GetNumberRows() - text_file.GetLineCount());
        }

        grid->ClearGrid();

        for (int row = 0; row < text_file.GetLineCount(); ++row) {
            wxStringTokenizer tokenizer(text_file[row], L",");
            for (int col = 0; tokenizer.HasMoreTokens() && col < grid->GetNumberCols(); ++col) {
                wxString token = tokenizer.GetNextToken();

                grid->SetCellValue(row, col, token);
            }
        }
        grid->AutoSize();

        text->Clear();
        lines.clear();

        for (int row = 0; row < text_file.GetLineCount(); ++row) {
            lines.push_back(text_file[row]);
        }

        text_file.Close();

        topsizer->Layout();
        SetStatusText(wxFileName(filename).GetFullName());
    }
}
void PopulateWXGridFromDataGrid(const CDataGrid& rSrcDataGrid, wxGrid& rDestWXGrid)
{
    // Remove old rows, and insert new ones as appropriate
    rDestWXGrid.DeleteCols(0, rDestWXGrid.GetNumberCols());
    rDestWXGrid.DeleteRows(0, rDestWXGrid.GetNumberRows());
    rDestWXGrid.AppendCols(rSrcDataGrid.GetNumberCols());
    rDestWXGrid.AppendRows(rSrcDataGrid.GetNumberRows());
    rDestWXGrid.SetDefaultCellTextColour(wxColour(0, 0, 0));
    rDestWXGrid.SetLabelTextColour(wxColour(0, 0, 0));
    rDestWXGrid.SetRowLabelSize(0);
    rDestWXGrid.EnableDragRowSize(false);

    // Top Headings
    for (int iCol = 0; iCol < rSrcDataGrid.GetNumberCols(); iCol++)
    {
        rDestWXGrid.SetColLabelValue(iCol, wxString(rSrcDataGrid.GetTopHeadingText(iCol), wxConvUTF8));
    }

    for (int iRow = 0; iRow < rSrcDataGrid.GetNumberRows(); iRow++)
    {
        // Side Heading
        rDestWXGrid.SetRowLabelValue(iRow, wxString(rSrcDataGrid.GetSideHeadingText(iRow), wxConvUTF8));

        // Data Row
        for (int iCol = 0; iCol < rSrcDataGrid.GetNumberCols(); iCol++)
        {
            rDestWXGrid.SetCellValue(iRow, iCol, wxString(rSrcDataGrid.GetCellText(iRow, iCol), wxConvUTF8));
            rDestWXGrid.SetReadOnly(iRow, iCol, true);

            wxFont vCellFont = rDestWXGrid.GetCellFont(iRow, iCol);
            if (rSrcDataGrid.IsCellBold(iRow, iCol))
                vCellFont.SetWeight(wxFONTWEIGHT_BOLD);
            if (rSrcDataGrid.IsCellUnderlined(iRow, iCol))
                vCellFont.SetUnderlined(true);
            rDestWXGrid.SetCellFont(iRow, iCol, vCellFont);
        }
    }

    rDestWXGrid.AutoSizeColumns(false);
    rDestWXGrid.AutoSizeRows(false);
}