You can view a database table in a grid using this class.
If you are deriving your own wxDbTable subclass for your table , then you may consider overriding GetCol() and SetCol() to provide calculated fields. This does work but care should be taken when using wxDbGridTableBase in this way.
The constructor and AssignDbTable() call allows you to specify the ownership if the wxDbTable object pointer. If you tell wxGridTableBase to take ownership , it will delete the passed wxDbTable when an new on is assigned or wxGridTableBase's destructor is called. However no checks for aliasing are done so Assign(table,..,true); Assign(table,..,true); is an error. If you need to requery an table object the preferred way is that the client keeps ownership.
Derived From
Include files
<wx/dbgrid.h>
Example
// First step, let's define wxDbTable int numColumns = 2; wxDbTable *table = new wxDbTable (db, tblName, numColumns); int int_var; wxChar string_name[255]; table->SetColDef (0, "column 0", DB_DATA_TYPE_INTEGER, &int_var, SQL_C_LONG, sizeof(int_var), true); table->SetColDef (1, "column 1", DB_DATA_TYPE_VARCHAR, &string_name, SQL_C_LONG, sizeof(string_name), false); // now let's define columns in the grid // first way to do it wxDbGridColInfo *columns; columns = new wxDbGridColInfo(0, wxGRID_VALUE_LONG, "first column", new wxDbGridColInfo(1, wxGRID_VALUE_STRING, "second column", NULL); // second way to do it wxDbGridColInfo *columns; // first column is special columns = new wxDbGridColInfo(0, wxGRID_VALUE_LONG, "first column", NULL); // all the rest columns->AddColInfo (1, wxGRID_VALUE_STRING, "second column"); // second way may be better when columns are not known at compile time // now, let's open the table and make a Query() table->Open(); // this step is very important table->SetRowMode (wxDbTable::WX_ROW_MODE_QUERY); // in the grid we will see only the rows of the result query m_dbTable->Query(); wxDbGridTableBase *dbgrid = new wxDbGridTableBase(table, columns, wxUSE_QUERY, true); delete columns; // not needed anymore wxGrid *grid = new wxGrid ( ... ); grid->SetTable(dbgrid, true); grid->Fit();Include files
<wx/dbgrid.h>
Helper classes and data structures
wxDbGridTableBase::wxDbGridTableBase
wxDbGridTableBase::ValidateRow
wxDbGridTableBase::UpdateRow
wxDbGridTableBase::AssignDbTable
wxDbGridTableBase(wxDbTable *tab, wxDbGridColInfo *ColInfo, int count = wxUSE_QUERY, bool takeOwnership = true)
Constructor.
Parameters
tab
takeOwnership
void ValidateRow(int row)
It ensures that the row data is fetched from the database, and it the wxDbTable local buffer, the row number passed should be the grid row.
Parameters
row
bool UpdateRow(int row)
If row has changed it forces that row to be written back to the database, however support for detecting whether insert/update is required is currently not in wxDbTable, so this function is currently unsupported.
Parameters
row
bool AssignDbTable(wxDbTable *tab,int count = wxUSE_QUERY, bool takeOwnership = true)
Resets the grid for using with a new database table, but using the same columns definition. This can be useful when re-querying the database and want to see the changes.
Parameters
tab